自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

VB.NET Split使用方法寶典

開(kāi)發(fā) 后端
VB.NET Split使用方法你都知道嗎?在這里我們?yōu)槟憬榻B三種方法:1簡(jiǎn)單的split分割字符串,2分割一個(gè)完整文件路徑,3根據(jù)單詞分割語(yǔ)句 這里用了正則表達(dá)式。

在學(xué)習(xí)中我們要善于總結(jié),總結(jié)對(duì)我們有很大的幫助,可以加快我們學(xué)習(xí)的步伐。在這里我給大家總結(jié)一下關(guān)于VB.NET Split使用方法,希望朋友們?cè)诠ぷ鲗W(xué)習(xí)中總結(jié)出更多的方法。

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

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

  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使用方法二. 分割一個(gè)完整文件路徑

  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使用方法三. 根據(jù)單詞分割語(yǔ)句 這里用了正則表達(dá)式

  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 ---- 這個(gè)是第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使用方法四. 分割一個(gè)文本文件中的每行

  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繪圖方法的三個(gè)方面
  2. 你是否了解VB.NET集成開(kāi)發(fā)環(huán)境
  3. 簡(jiǎn)單談?wù)揤B.NET傳輸表空間
  4. 淺析VB.NET語(yǔ)言與VB語(yǔ)言對(duì)比
  5. 五大類(lèi)VB.NET運(yùn)算符全面介紹
責(zé)任編輯:田樹(shù) 來(lái)源: 樂(lè)博網(wǎng)
相關(guān)推薦

2010-01-20 17:47:54

VB.NET注釋

2010-01-21 14:06:03

VB.NET MyCl

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-18 13:12:43

VB.NET控件數(shù)組

2010-01-20 13:28:35

VB.NET計(jì)算數(shù)字

2009-10-27 13:34:32

VB.NET WEB服

2009-11-02 15:08:58

VB.NET Obje

2009-11-02 13:14:18

VB.NET函數(shù)

2010-01-20 10:27:07

VB.NET隱式類(lèi)型局

2009-10-16 13:38:43

VB.NET Spli

2009-11-03 09:48:47

VB.NET構(gòu)造

2009-10-13 15:20:02

VB.NET使用Dra

2009-10-28 17:08:57

VB.NET數(shù)據(jù)庫(kù)開(kāi)發(fā)

2009-11-03 09:26:13

VB.NET方法

2009-10-30 09:45:55

VB.NET Web

2009-10-26 18:41:05

VB.NET獲取硬盤(pán)信

2010-01-11 16:04:10

VB.NET使用wit
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)