您现在的位置: 365建站网 > 365文章 > vb.net split字符串分割函数的用法

vb.net split字符串分割函数的用法

文章来源:365jz.com     点击数:3924    更新时间:2018-06-04 08:09   参与评论

VB.NET Split函数使你能够将长字符串分离为单独的字;但是如果在字与字之间不止一个空格,Split就会返回一个错误的结果。为了防止这种情况发生,你可以在使用Split之前用Replace函数来替换多个空格的出现。列表A给出了一个例子。

名称说明
Split(Char[])

基于数组中的字符将字符串拆分为多个子字符串。


Split(Char[], Int32)

基于数组中的字符将一个字符串拆分成最大数量的子字符串。 也可指定要返回的子字符串的最大数量。


Split(Char[], Int32, StringSplitOptions)

基于数组中的字符将一个字符串拆分成最大数量的子字符串。


Split(Char[], StringSplitOptions)

基于数组中的字符将字符串拆分为多个子字符串。 可以指定子字符串是否包含空数组元素。


Split(String[], Int32, StringSplitOptions)

基于数组中的字符串将一个字符串拆分成最大数量的子字符串。 可以指定子字符串是否包含空数组元素。


Split(String[], StringSplitOptions)

基于数组中的字符串将字符串拆分为多个子字符串。 可以指定子字符串是否包含空数组元素。


</>code

  1. Private Sub CountWords()  
  2. Dim strText As String = "It's a wonderfulworld" 
  3. Dim iCount As Integer  
  4.  
  5. Do While (strText.IndexOf(Space(2)) >= 0)  
  6. strTextstrText = strText.Replace(Space(2), Space(1))  
  7. Loop  
  8. iCount = Split(strText, Space(1)).Length  
  9. MsgBox(iCount.ToString())  
  10. End Sub

在这个例子中,我创建了字符串strText,再将它设置成有多个字符的长字符串。然后,我利用Replace函数来把出现的多个空格替换成一个空格。这样做是为了把字符串strText准备就绪,让你能够使用VB.NET Split函数并提供正确的结果。接着,我将strText输入Split函数,并且得到了包括在字符串strText中的字数。

注意:如果你跳过或注释用来移除多余空格的循环,结果是7个字。使用移除所有多余空格的循环后,结果才是正确的,4个字。



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

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

</>code

  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使用方法二. 分割一个完整文件路径

</>code

  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使用方法三. 根据单词分割语句 这里用了正则表达式

</>code

  1. Imports System.Text.RegularExpressions  
  2. Module Module1  
  3. Sub Main()  
  4. ' Declare iteration variable  
  5. Dim s As String  
  6. ' Loop through words in string  
  7. Dim arr As String() = SplitWords(".Net Fans's QQ group No is 40797788, man!")  
  8. ' Display each word. Note that punctuation is handled correctly.  
  9. For Each s In arr  
  10. Console.WriteLine(s)  
  11. Next  
  12. Console.ReadLine()  
  13. End Sub  
  14. ''' <summary> 
  15. ''' Split the words in string on non-word characters.  
  16. ''' This means commas and periods are handled correctly.  
  17. ''' summary> 
  18. Private Function SplitWords(ByVal s As String) As String()  
  19. '  
  20. ' Call Regex.Split function from the imported namespace.  
  21. ' Return the result array.  
  22. 'Return Regex.Split(s, "\W+")  
  23. End Function  
  24. End Module 
  25. === Output of the program ===  
  26. 第一行是空白  
  27. Net ---- 这个是第2行了  
  28. Fans  
  29. s   
  30. QQ   
  31. group   
  32. No   
  33. is   
  34. 40797788  
  35. man

 

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

</>code

  1. Imports System.IO  
  2. Module Module1  
  3. Sub Main()  
  4. Dim i As Integer = 0 
  5. 'vb.net  
  6. ' Loop through each line in array returned by ReadAllLines  
  7. Dim line As String  
  8. For Each line In File.ReadAllLines("example.txt")  
  9. ' Split line on comma  
  10. Dim parts As String() = line.Split(New Char() {","c})  
  11. ' Loop over each string received  
  12. Dim part As String  
  13. For Each part In parts  
  14. ' Display to Console  
  15. Console.WriteLine("{0}:{1}", i, part)  
  16. Next  
  17. i += 1  
  18. Next  
  19. End Sub  
  20. End Module 
  21. === Output of the program ===  
  22. 0:frontal  
  23. 0:parietal  
  24. 0:occipital  
  25. 0:temporal  
  26. 1:pulmonary artery  
  27. 1:aorta  
  28. 1:left ventricle

 



如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (3924人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号