字符在字符串中当前位置左侧的位置

从字符串中的某个任意位置,我需要找到一个字符在我位置左侧的最近位置。 如果我想要向右执行此操作,我可以使用.IndexOf,但如何在左侧执行此操作我不确定。

我提出的两种方法只是从我的位置开始的递减循环,或者使用正常的字符串反向放置字符串.IndexOf

还有其他人有更好的方法来实现这个目标吗?

关于什么:

yourstring.LastIndexOf("foo", 0, currentPosition) 

更明确地,在txt中找到nWord之前出现的单词:您的单词将位于位置s

 Dim s As Integer = txt.Substring(0, txt.IndexOf(nWord)).LastIndexOf(word) 

这对我来说很有用,我需要找到所有出现的循环。

这是如何构造循环:

 Dim n As Integer = 0 Dim s As Integer = 0 Do While txt.Contains(word) AndAlso txt.Contains(nWord) n = txt.IndexOf(nWord) 'n = txt.IndexOf(nWord)+nWord.Length ':If nWord may also contain word s += txt.Substring(0, n).LastIndexOf(word) txt = txt.SubString(n + nWord.Length) MsgBox("Found at " & s.ToString()) Loop