VB.NET Split函數(shù)使你能夠?qū)㈤L(zhǎng)字符串分離為單獨(dú)的字;但是如果在字與字之間不止一個(gè)空格,,Split就會(huì)返回一個(gè)錯(cuò)誤的結(jié)果,。為了防止這種情況發(fā)生,你可以在使用Split之前用Replace函數(shù)來替換多個(gè)空格的出現(xiàn),。列表A給出了一個(gè)例子。
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 在這個(gè)例子中,,我創(chuàng)建了字符串strText,再將它設(shè)置成有多個(gè)字符的長(zhǎng)字符串,。然后,,我利用Replace函數(shù)來把出現(xiàn)的多個(gè)空格替換成一個(gè)空格。這樣做是為了把字符串strText準(zhǔn)備就緒,,讓你能夠使用VB.NET Split函數(shù)并提供正確的結(jié)果,。接著,我將strText輸入Split函數(shù),,并且得到了包括在字符串strText中的字?jǐn)?shù),。 注意:如果你跳過或注釋用來移除多余空格的循環(huán),結(jié)果是7個(gè)字,。使用移除所有多余空格的循環(huán)后,,結(jié)果才是正確的,4個(gè)字,。
首先我們拿了一個(gè)帶多個(gè)空格的字符串來做分割. 我們分配一個(gè)New Char() array 保存為String() array 來儲(chǔ)存我們例子中的那些分割后的單詞.最后,,我們用loop循環(huán)來遍歷和顯示這些分割后的字母集合. === 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使用方法二. 分割一個(gè)完整文件路徑 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使用方法三. 根據(jù)單詞分割語(yǔ)句 這里用了正則表達(dá)式 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 ---- 這個(gè)是第2行了
Fans
s
QQ
group
No
is
40797788
man VB.NET Split使用方法四. 分割一個(gè)文本文件中的每行 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 |
|