在学习中我们要善于总结,总结对我们有很大的帮助,可以加快我们学习的步伐。在这里我给大家总结一下关于VB.NETSplit使用方法,希望朋友们在工作学习中总结出更多的方法。
VB.NETSplit使用方法一.简单的split分割字符串
首先我们拿了一个带多个空格的字符串来做分割.我们分配一个NewChar()array保存为String()array来储存我们例子中的那些分割后的单词.最后,我们用loop循环来遍历和显示这些分割后的字母集合.
===ProgramthatusesSplitonString(VB.NET)===
ModuleModule1
SubMain()
'Wewanttosplitthisinputstring
DimsAsString="Ourwebsiteaddressiswww.51cto.cn"
'Splitstringbasedonspaces
DimwordsAsString()=s.Split(NewChar(){""c})
'UseForEachloopoverwordsanddisplaythem
DimwordAsString
ForEachwordInwords
Console.WriteLine(word)
Next
EndSub
EndModule
===Outputoftheprogram===
Our
website
address
is
VB.NETSplit使用方法二.分割一个完整文件路径
HereweseehowyoucanSplitafilesystempathintoseparatepartsusingVisualBasic.NET.WeuseaNewChar()arraywithonestring,"""c,andthenloopthroughanddisplaytheresults.
===Programthatsplitsfilepath(VB.NET)===
ModuleModule1
SubMain()
'Thefilesystempathweneedtosplit
DimsAsString="C:UsersSamDocumentsPerls51cto"
'Splitthestringonthebackslashcharacter
DimpartsAsString()=s.Split(NewChar(){""c})
'LoopthroughresultstringswithForEach
DimpartAsString
ForEachpartInparts
Console.WriteLine(part)
Next
EndSub
EndModule
===Outputoftheprogram===
C:
Users
Sam
Documents
Perls
VB.NETSplit使用方法三.根据单词分割语句这里用了正则表达式
OftenyouneedtoextractthewordsfromaStringorsentenceinVB.NET.Thecodehereneedstohandlepunctuation andnon-wordcharactersdifferentlythantheStringSplitmethod.HereweuseRegex.Splittoparsethewords.
===Programthatsplitswords(VB.NET)===
ImportsSystem.Text.RegularExpressions
ModuleModule1
SubMain()
'Declareiterationvariable
DimsAsString
'Loopthroughwordsinstring
DimarrAsString()=SplitWords(".NetFans'sQQgroupNois40797788,man!")
'Displayeachword.Notethatpunctuationishandledcorrectly.
ForEachsInarr
Console.WriteLine(s)
Next
Console.ReadLine()
EndSub
'''<summary>
'''Splitthewordsinstringonnon-wordcharacters.
'''Thismeanscommasandperiodsarehandledcorrectly.
'''<<span><< span>
summary>
PrivateFunctionSplitWords(ByValsAsString)AsString()
'
'CallRegex.Splitfunctionfromtheimportednamespace.
'Returntheresultarray.
'ReturnRegex.Split(s,"W+")
EndFunction
EndModule
===Outputoftheprogram===
第一行是空白
Net----这个是第2行了
Fans
s
QQ
group
No
is
40797788
man
Descriptionoftheexamplecode.IntheMain()subroutineyoucansee thattwovariablesaredeclared.ThesecondvariableisaString()arraythatreceivestheresultsfromthePrivateFunctionnext.
DescriptionoftheRegex.TheFunctionshownintheexamplecalls theRegex.Splitmethod,whichcanbeaccessedwithdotnotation,withnoinstancenecessary. Thesecondparametertothemethodisaregularexpressionpattern.
DescriptionoftheRegexpattern.Thepattern"W+"isused,andthismeans"1ormorenon-wordcharacters". Thispatternwillmatchpunctuationandspaces.Therefore,allthosecharacterswillbeusedasdelimiters.
VB.NETSplit使用方法四.分割一个文本文件中的每行
HereweseeonewaytoSpliteachlineinafileusingFile.ReadAllLinesandSplit. Wehaveacomma-separated-valuesCSVfile,andwanttoprintouteachvalueanditsrownumber. Hereistheinputfile"example.txt".[Seebelow]
TheSplitcodeexamplefollows.ItfirstreadsinthefilewithReadAllLines. Thisfunctionputseachlineinthefileintoanarrayelement.TheexamplenextSplitson","c. Thefinalcommentshowstheoutputoftheprogram.
===Inputfileused===
frontal,parietal,occipital,temporal
pulmonaryartery,aorta,leftventricle
===Exampleprogramthatsplitslines(VB.NET)===
ImportsSystem.IO
ModuleModule1
SubMain()
DimiAsInteger=0
'vb.net
'LoopthrougheachlineinarrayreturnedbyReadAllLines
DimlineAsString
ForEachlineInFile.ReadAllLines("example.txt")
'Splitlineoncomma
DimpartsAsString()=line.Split(NewChar(){","c})
'Loopovereachstringreceived
DimpartAsString
ForEachpartInparts
'DisplaytoConsole
Console.WriteLine("{0}:{1}",i,part)
Next
i+=1
Next
EndSub
EndModule
===Outputoftheprogram===
0:frontal
0:parietal
0:occipital
0:temporal
1:pulmonaryartery
1:aorta
1:leftventricle
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|