Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

c# - Alternative to using InStr

how can I use a different function "InStr" this is the code that I am using and works fine but moving away from InStr is my goal

i = InStr(1, Hostname, Environment.Newline)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

String.Indexof() with several overloads:

Dim jstr = "How much wood could a woodchuck chuck if a woodchuck..."

' Find a character from a starting point
ndx = jstr.IndexOf("w"c)             ' == 2 (first w)
'  or within a range:
ndx = jstr.IndexOf("o"c, 12)         ' == 15 first o past 12 (cOuld)  

'Find a string
ndx = jstr.IndexOf("wood")           ' == 9
' ...from a starting point
ndx = jstr.IndexOf("wood", 10)       ' == 22 (WOODchuck)
' ...or in part of the string
ndx = jstr.IndexOf("chuck", 9, 15)   ' -1 (none in that range)

' using a specified comparison method:
ndx = jstr.IndexOf("WOOD", StringComparison.InvariantCultureIgnoreCase)  ' 9
ndx = jstr.IndexOf("WOOD", nFirst, StringComparison) 
ndx = jstr.IndexOf("WOOD", nFirst, nLast, StringComparison)

There is also a String,LastIndexOf() method to get the last occurance of something in a string also with a variety of overloads.

Available in MSDN or Object Browser (VIEW menu | Object Browser) in the VS near you.

i = Hostname.Indexof(Environment.Newline, 1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...