五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > vb.net split字符串分割函數(shù)的用法

vb.net split字符串分割函數(shù)的用法

文章來(lái)源:365jz.com     點(diǎn)擊數(shù):3952    更新時(shí)間:2018-06-04 08:09   參與評(píng)論

VB.NET Split函數(shù)使你能夠?qū)㈤L(zhǎng)字符串分離為單獨(dú)的字;但是如果在字與字之間不止一個(gè)空格,Split就會(huì)返回一個(gè)錯(cuò)誤的結(jié)果。為了防止這種情況發(fā)生,你可以在使用Split之前用Replace函數(shù)來(lái)替換多個(gè)空格的出現(xiàn)。列表A給出了一個(gè)例子。

名稱說明
Split(Char[])

基于數(shù)組中的字符將字符串拆分為多個(gè)子字符串。


Split(Char[],?Int32)

基于數(shù)組中的字符將一個(gè)字符串拆分成最大數(shù)量的子字符串。 也可指定要返回的子字符串的最大數(shù)量。


Split(Char[],?Int32,?StringSplitOptions)

基于數(shù)組中的字符將一個(gè)字符串拆分成最大數(shù)量的子字符串。


Split(Char[],?StringSplitOptions)

基于數(shù)組中的字符將字符串拆分為多個(gè)子字符串。 可以指定子字符串是否包含空數(shù)組元素。


Split(String[],?Int32,?StringSplitOptions)

基于數(shù)組中的字符串將一個(gè)字符串拆分成最大數(shù)量的子字符串。 可以指定子字符串是否包含空數(shù)組元素。


Split(String[],?StringSplitOptions)

基于數(shù)組中的字符串將字符串拆分為多個(gè)子字符串。 可以指定子字符串是否包含空數(shù)組元素。


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ù)來(lái)把出現(xiàn)的多個(gè)空格替換成一個(gè)空格。這樣做是為了把字符串strText準(zhǔn)備就緒,讓你能夠使用VB.NET Split函數(shù)并提供正確的結(jié)果。接著,我將strText輸入Split函數(shù),并且得到了包括在字符串strText中的字?jǐn)?shù)。

注意:如果你跳過或注釋用來(lái)移除多余空格的循環(huán),結(jié)果是7個(gè)字。使用移除所有多余空格的循環(huán)后,結(jié)果才是正確的,4個(gè)字。



VB.NET Split使用方法一. 簡(jiǎn)單的split分割字符串

首先我們拿了一個(gè)帶多個(gè)空格的字符串來(lái)做分割. 我們分配一個(gè)New Char() array 保存為String() array 來(lái)儲(chǔ)存我們例子中的那些分割后的單詞.最后,我們用loop循環(huán)來(lái)遍歷和顯示這些分割后的字母集合.

=== 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

 



如對(duì)本文有疑問,請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答??! 點(diǎn)擊進(jìn)入論壇

發(fā)表評(píng)論 (3952人查看,0條評(píng)論)
請(qǐng)自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴(yán)禁發(fā)布色情、暴力、反動(dòng)的言論。
昵稱:
最新評(píng)論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時(shí)間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)