2010-01-07 69 views
1

從字符串中的某個任意位置開始,我需要在我的位置左側找到一個字符的最近位置。如果我想要執行此操作的權利,我可以使用.IndexOf,但如何做到左側,我不確定。字符在字符串中當前位置左側的位置

我來到了剛剛遞減循環開始在我的位置,或者把字符串中的反向和使用普通.IndexOf

任何人有這個acheiving任何更好的方法將兩種方式?

回答

7

什麼:

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

有關嘛怎麼樣了,到目前爲止,我正努力爭取一個更簡單的解決方案。 – CMN

0

更明確地說,找到發生 nWordTXT:你的話將在位置小號

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