替代使用InStr

我怎么能使用不同的function“InStr”这是我正在使用的代码并且工作正常但是离开InStr是我的目标

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

具有多个重载的String.Indexof()

 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) 

还有一个String,LastIndexOf()方法来获取字符串中某些内容的最后一次出现,同时还有各种重载。

在您附近的VS中的MSDN或对象浏览器(VIEW菜单|对象浏览器)中可用。

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

如果您需要等效的C#代码,可以使用Microsoft.VisualBasic程序Strings类,因此代码可以如下所示:

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

在此处输入图像描述

另一种方法是使用适当的String.Indexof函数。

链接: