Alternative to using InStr

how can I use another "InStr" function, this is the code that I use and works great, but getting away from InStr is my goal.

i = InStr(1, Hostname, Environment.Newline) 
+7
c #
source share
2 answers

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 String,LastIndexOf() to get the last occurrence of something on a string and with a lot of overloads.

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

 i = Hostname.Indexof(Environment.Newline, 1) 
+16
source share

If you need equivalent C # code, you can use the Strings class from the Microsoft.VisualBasic assembly, so the code might look like this:

 using Microsoft.VisualBasic; . . . i = Strings.InStr(1, Hostname, Environment.NewLine); 

enter image description here

Another approach uses the corresponding String.Indexof function.

References:

+5
source share

All Articles