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) | 基于数组中的字符串将字符串拆分为多个子字符串。 可以指定子字符串是否包含空数组元素。 |
Private Sub CountWords() Dim strText As String = "It's a wonderfulworld" Dim iCount As Integer Do While (strText.IndexOf(Space(2)) >= 0) strTextstrText = strText.Replace(Space(2), Space(1)) Loop iCount = Split(strText, Space(1)).Length MsgBox(iCount.ToString()) End Sub
在这个例子中,我创建了字符串strText,再将它设置成有多个字符的长字符串。然后,我利用Replace函数来把出现的多个空格替换成一个空格。这样做是为了把字符串strText准备就绪,让你能够使用VB.NET Split函数并提供正确的结果。接着,我将strText输入Split函数,并且得到了包括在字符串strText中的字数。
注意:如果你跳过或注释用来移除多余空格的循环,结果是7个字。使用移除所有多余空格的循环后,结果才是正确的,4个字。
VB.NET Split使用方法一. 简单的split分割字符串
首先我们拿了一个带多个空格的字符串来做分割. 我们分配一个New Char() array 保存为String() array 来储存我们例子中的那些分割后的单词.最后,我们用loop循环来遍历和显示这些分割后的字母集合.
=== Program that uses Split on String (VB.NET) === Module Module1 Sub Main() ' We want to split this input string Dim s As String = "Our website address is www.51cto.cn" ' Split string based on spaces Dim words As String() = s.Split(New Char() {" "c}) ' Use For Each loop over words and display them Dim word As String For Each word In words Console.WriteLine(word) Next End Sub End Module === Output of the program === Our website address is
VB.NET Split使用方法二. 分割一个完整文件路径
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. === Program that splits file path (VB.NET) === Module Module1 Sub Main() ' The file system path we need to split Dim s As String = "C:\Users\Sam\Documents\Perls\51cto" ' Split the string on the backslash character Dim parts As String() = s.Split(New Char() {"\"c}) ' Loop through result strings with For Each Dim part As String For Each part In parts Console.WriteLine(part) Next End Sub End Module === Output of the program === C: Users Sam Documents Perls
VB.NET Split使用方法三. 根据单词分割语句 这里用了正则表达式
Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Declare iteration variable Dim s As String ' Loop through words in string Dim arr As String() = SplitWords(".Net Fans's QQ group No is 40797788, man!") ' Display each word. Note that punctuation is handled correctly. For Each s In arr Console.WriteLine(s) Next Console.ReadLine() End Sub ''' <summary> ''' Split the words in string on non-word characters. ''' This means commas and periods are handled correctly. ''' summary> Private Function SplitWords(ByVal s As String) As String() ' ' Call Regex.Split function from the imported namespace. ' Return the result array. 'Return Regex.Split(s, "\W+") End Function End Module === Output of the program === 第一行是空白 Net ---- 这个是第2行了 Fans s QQ group No is 40797788 man
VB.NET Split使用方法四. 分割一个文本文件中的每行
Imports System.IO Module Module1 Sub Main() Dim i As Integer = 0 'vb.net ' Loop through each line in array returned by ReadAllLines Dim line As String For Each line In File.ReadAllLines("example.txt") ' Split line on comma Dim parts As String() = line.Split(New Char() {","c}) ' Loop over each string received Dim part As String For Each part In parts ' Display to Console Console.WriteLine("{0}:{1}", i, part) Next i += 1 Next End Sub End Module === Output of the program === 0:frontal 0:parietal 0:occipital 0:temporal 1:pulmonary artery 1:aorta 1:left ventricle
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛