VB.NET Split使用方法宝典

开发 后端
VB.NET Split使用方法你都知道吗?在这里我们为你介绍三种方法:1简单的split分割字符串,2分割一个完整文件路径,3根据单词分割语句 这里用了正则表达式。

在学习中我们要善于总结,总结对我们有很大的帮助,可以加快我们学习的步伐。在这里我给大家总结一下关于VB.NET Split使用方法,希望朋友们在工作学习中总结出更多的方法。

VB.NET Split使用方法一. 简单的split分割字符串

首先我们拿了一个带多个空格的字符串来做分割. 我们分配一个New Char() array 保存为String() array 来储存我们例子中的那些分割后的单词.最后,我们用loop循环来遍历和显示这些分割后的字母集合.

  1. === Program that uses Split on String (VB.NET) ===  
  2. Module Module1  
  3. Sub Main()  
  4. ' We want to split this input string  
  5. Dim s As String = "Our website address is www.51cto.cn" 
  6. ' Split string based on spaces  
  7. Dim words As String() = s.Split(New Char() {" "c})  
  8. ' Use For Each loop over words and display them  
  9. Dim word As String  
  10. For Each word In words  
  11. Console.WriteLine(word)  
  12. Next  
  13. End Sub  
  14. End Module 
  15. === Output of the program ===  
  16. Our   
  17. website   
  18. address   
  19. is  

VB.NET Split使用方法二. 分割一个完整文件路径

  1. Here we see how you can Split a file system path into separate parts using Visual Basic .NET. We use a New Char() array with one string, "\""c, and then loop through and display the results. 
  2. === Program that splits file path (VB.NET) ===  
  3. Module Module1  
  4. Sub Main()  
  5. ' The file system path we need to split  
  6. Dim s As String = "C:\Users\Sam\Documents\Perls\51cto" 
  7. ' Split the string on the backslash character  
  8. Dim parts As String() = s.Split(New Char() {"\"c})  
  9. ' Loop through result strings with For Each  
  10. Dim part As String  
  11. For Each part In parts  
  12. Console.WriteLine(part)  
  13. Next  
  14. End Sub  
  15. End Module 
  16. === Output of the program ===  
  17. C:  
  18. Users  
  19. Sam  
  20. Documents  
  21. Perls  

VB.NET Split使用方法三. 根据单词分割语句 这里用了正则表达式

  1. Often you need to extract the words from a String or sentence in VB.NET. The code here needs to handle punctuation 
    and non-word characters differently than the String Split method. Here we use Regex.Split to parse the 
    words. 
  2. === Program that splits words (VB.NET) ===  
  3. Imports System.Text.RegularExpressions  
  4. Module Module1  
  5. Sub Main()  
  6. ' Declare iteration variable  
  7. Dim s As String  
  8. ' Loop through words in string  
  9. Dim arr As String() = SplitWords(".Net Fans's QQ group No is 40797788, man!")  
  10. ' Display each word. Note that punctuation is handled correctly.  
  11. For Each s In arr  
  12. Console.WriteLine(s)  
  13. Next  
  14. Console.ReadLine()  
  15. End Sub  
  16. ''' <summary> 
  17. ''' Split the words in string on non-word characters.  
  18. ''' This means commas and periods are handled correctly.  
  19. ''' summary> 
  20. Private Function SplitWords(ByVal s As String) As String()  
  21. '  
  22. ' Call Regex.Split function from the imported namespace.  
  23. ' Return the result array.  
  24. 'Return Regex.Split(s, "\W+")  
  25. End Function  
  26. End Module 
  27. === Output of the program ===  
  28. 第一行是空白  
  29. Net ---- 这个是第2行了  
  30. Fans  
  31. s   
  32. QQ   
  33. group   
  34. No   
  35. is   
  36. 40797788  
  37. man  
  38. Description of the example code. In the Main() subroutine you can see 
    that two variables are declared. The second variable is a String() array that receives the results from the Private Function next.   
  39. Description of the Regex. The Function shown in the example calls 
    the Regex.Split method, which can be accessed with dot notation, with no instance necessary. 
    The second parameter to the method is a regular expression pattern.   
  40. Description of the Regex pattern. The pattern "\W+" is used, and this means "1 or more non-word characters".
     This pattern will match punctuation and spaces. Therefore, all those characters will be used as delimiters.  

VB.NET Split使用方法四. 分割一个文本文件中的每行

  1. Here we see one way to Split each line in a file using File.ReadAllLines and Split.
     We have a comma-separated-values CSV file, and want to print out each value and its row number. 
    Here is the input file "example.txt". [See below]   
  2. The Split code example follows. It first reads in the file with ReadAllLines. 
    This function puts each line in the file into an array element. The example next Splits on ","c. 
    The final comment shows the output of the 
    program. 
  3. === Input file used ===  
  4. frontal,parietal,occipital,temporal  
  5. pulmonary artery,aorta,left ventricle 
  6. === Example program that splits lines (VB.NET) ===  
  7. Imports System.IO  
  8. Module Module1  
  9. Sub Main()  
  10. Dim i As Integer = 0 
  11. 'vb.net  
  12. ' Loop through each line in array returned by ReadAllLines  
  13. Dim line As String  
  14. For Each line In File.ReadAllLines("example.txt")  
  15. ' Split line on comma  
  16. Dim parts As String() = line.Split(New Char() {","c})  
  17. ' Loop over each string received  
  18. Dim part As String  
  19. For Each part In parts  
  20. ' Display to Console  
  21. Console.WriteLine("{0}:{1}", i, part)  
  22. Next  
  23. i += 1  
  24. Next  
  25. End Sub  
  26. End Module 
  27. === Output of the program ===  
  28. 0:frontal  
  29. 0:parietal  
  30. 0:occipital  
  31. 0:temporal  
  32. 1:pulmonary artery  
  33. 1:aorta  
  34. 1:left ventricle  

【编辑推荐】

  1. 介绍VB.NET绘图方法的三个方面
  2. 你是否了解VB.NET集成开发环境
  3. 简单谈论VB.NET传输表空间
  4. 浅析VB.NET语言与VB语言对比
  5. 五大类VB.NET运算符全面介绍
责任编辑:田树 来源: 乐博网
相关推荐

2010-01-21 14:06:03

VB.NET MyCl

2010-01-20 17:47:54

VB.NET注释

2010-01-21 17:23:05

VB.NET Radi

2010-01-19 14:50:20

VB.NET集合

2010-01-19 09:36:06

VB.NET Func

2009-10-15 17:50:48

VB.NET Spli

2010-01-20 13:28:35

VB.NET计算数字

2010-01-18 13:12:43

VB.NET控件数组

2009-11-02 15:08:58

VB.NET Obje

2009-10-27 13:34:32

VB.NET WEB服

2009-11-02 13:14:18

VB.NET函数

2010-01-20 10:27:07

VB.NET隐式类型局

2009-10-16 13:38:43

VB.NET Spli

2009-10-13 15:20:02

VB.NET使用Dra

2009-11-03 09:48:47

VB.NET构造

2009-10-28 17:08:57

VB.NET数据库开发

2009-10-30 09:45:55

VB.NET Web

2009-10-26 18:41:05

VB.NET获取硬盘信

2009-11-03 09:26:13

VB.NET方法

2009-10-14 17:08:44

VB.NET使用Fil
点赞
收藏

51CTO技术栈公众号