VB.NET Split函数的应用

在学习中我们要善于总结,总结对我们有很大的帮助,可以加快我们学习的步伐。在这里我给大家总结一下关于VB.NETSplit使用方法,希望朋友们在工作学习中总结出更多的方法

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

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

 
 
  1. ===ProgramthatusesSplitonString(VB.NET)===

  2. ModuleModule1

  3. SubMain()

  4. 'Wewanttosplitthisinputstring

  5. DimsAsString="Ourwebsiteaddressiswww.51cto.cn"

  6. 'Splitstringbasedonspaces

  7. DimwordsAsString()=s.Split(NewChar(){""c})

  8. 'UseForEachloopoverwordsanddisplaythem

  9. DimwordAsString

  10. ForEachwordInwords

  11. Console.WriteLine(word)

  12. Next

  13. EndSub

  14. EndModule

  15. ===Outputoftheprogram===

  16. Our

  17. website

  18. address

  19. is

VB.NETSplit使用方法二.分割一个完整文件路径

 
 
  1. HereweseehowyoucanSplitafilesystempathintoseparatepartsusingVisualBasic.NET.WeuseaNewChar()arraywithonestring,"\""c,andthenloopthroughanddisplaytheresults.

  2. ===Programthatsplitsfilepath(VB.NET)===

  3. ModuleModule1

  4. SubMain()

  5. 'Thefilesystempathweneedtosplit

  6. DimsAsString="C:\Users\Sam\Documents\Perls\51cto"

  7. 'Splitthestringonthebackslashcharacter

  8. DimpartsAsString()=s.Split(NewChar(){"\"c})

  9. 'LoopthroughresultstringswithForEach

  10. DimpartAsString

  11. ForEachpartInparts

  12. Console.WriteLine(part)

  13. Next

  14. EndSub

  15. EndModule

  16. ===Outputoftheprogram===

  17. C:

  18. Users

  19. Sam

  20. Documents

  21. Perls

VB.NETSplit使用方法三.根据单词分割语句这里用了正则表达式

 
 
  1. OftenyouneedtoextractthewordsfromaStringorsentenceinVB.NET.Thecodehereneedstohandlepunctuation
    andnon-wordcharactersdifferentlythantheStringSplitmethod.HereweuseRegex.Splittoparsethewords.

  2. ===Programthatsplitswords(VB.NET)===

  3. ImportsSystem.Text.RegularExpressions

  4. ModuleModule1

  5. SubMain()

  6. 'Declareiterationvariable

  7. DimsAsString

  8. 'Loopthroughwordsinstring

  9. DimarrAsString()=SplitWords(".NetFans'sQQgroupNois40797788,man!")

  10. 'Displayeachword.Notethatpunctuationishandledcorrectly.

  11. ForEachsInarr

  12. Console.WriteLine(s)

  13. Next

  14. Console.ReadLine()

  15. EndSub

  16. '''<summary>

  17. '''Splitthewordsinstringonnon-wordcharacters.

  18. '''Thismeanscommasandperiodsarehandledcorrectly.

  19. '''<<span><< span> summary>

  20. PrivateFunctionSplitWords(ByValsAsString)AsString()

  21. '

  22. 'CallRegex.Splitfunctionfromtheimportednamespace.

  23. 'Returntheresultarray.

  24. 'ReturnRegex.Split(s,"\W+")

  25. EndFunction

  26. EndModule

  27. ===Outputoftheprogram===

  28. 第一行是空白

  29. Net----这个是第2行了

  30. Fans

  31. s

  32. QQ

  33. group

  34. No

  35. is

  36. 40797788

  37. man

  38. Descriptionoftheexamplecode.IntheMain()subroutineyoucansee
    thattwovariablesaredeclared.ThesecondvariableisaString()arraythatreceivestheresultsfromthePrivateFunctionnext.

  39. DescriptionoftheRegex.TheFunctionshownintheexamplecalls
    theRegex.Splitmethod,whichcanbeaccessedwithdotnotation,withnoinstancenecessary.
    Thesecondparametertothemethodisaregularexpressionpattern.

  40. DescriptionoftheRegexpattern.Thepattern"\W+"isused,andthismeans"1ormorenon-wordcharacters".
    Thispatternwillmatchpunctuationandspaces.Therefore,allthosecharacterswillbeusedasdelimiters.

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

 
 
  1. HereweseeonewaytoSpliteachlineinafileusingFile.ReadAllLinesandSplit.
    Wehaveacomma-separated-valuesCSVfile,andwanttoprintouteachvalueanditsrownumber.
    Hereistheinputfile"example.txt".[Seebelow]

  2. TheSplitcodeexamplefollows.ItfirstreadsinthefilewithReadAllLines.
    Thisfunctionputseachlineinthefileintoanarrayelement.TheexamplenextSplitson","c.
    Thefinalcommentshowstheoutputoftheprogram.

  3. ===Inputfileused===

  4. frontal,parietal,occipital,temporal

  5. pulmonaryartery,aorta,leftventricle

  6. ===Exampleprogramthatsplitslines(VB.NET)===

  7. ImportsSystem.IO

  8. ModuleModule1

  9. SubMain()

  10. DimiAsInteger=0

  11. 'vb.net

  12. 'LoopthrougheachlineinarrayreturnedbyReadAllLines

  13. DimlineAsString

  14. ForEachlineInFile.ReadAllLines("example.txt")

  15. 'Splitlineoncomma

  16. DimpartsAsString()=line.Split(NewChar(){","c})

  17. 'Loopovereachstringreceived

  18. DimpartAsString

  19. ForEachpartInparts

  20. 'DisplaytoConsole

  21. Console.WriteLine("{0}:{1}",i,part)

  22. Next

  23. i+=1

  24. Next

  25. EndSub

  26. EndModule

  27. ===Outputoftheprogram===

  28. 0:frontal

  29. 0:parietal

  30. 0:occipital

  31. 0:temporal

  32. 1:pulmonaryartery

  33. 1:aorta

  34. 1:leftventricle

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...