最快的方法是使用一個行正則表達式文本框。
Dim Output As String = Regex.Match(myInput, "\p{L}+(?=ing[^\p{L}])", RegexOptions.IgnoreCase).Value
A Regex
是一個能夠根據模式匹配字符串的類。這很好用,通常非常快。該模式是我已經傳遞給Match()
方法的第二個字符串。我的模式是這樣的:
\p{L}+
部分意味着它應該匹配每個字符是一個Unicode字母。 +
表示它應該匹配一個或多個字母。
(?=ing[^\p{L}])
部分表示匹配必須以「ing」結尾,並且它之後沒有任何Unicode字母。
要匹配多個動詞,我們必須擴大一點。把它放到一個函數中。該函數將查找與指定模式匹配的所有子字符串,然後將它們放入字符串數組中並將其返回給您。
Public Function FindVerbs(ByVal Input As String) As String()
Dim Matches As MatchCollection = Regex.Matches(Input, "\p{L}+(?=ing[^\p{L}])", RegexOptions.IgnoreCase)
Dim ReturnArray(Matches.Count - 1) As String
For x = 0 To Matches.Count - 1
ReturnArray(x) = Matches(x).Value
Next
Return ReturnArray
End Function
功能示例用法:
Dim Verbs() As String = FindVerbs("I am playing with my helicopter. It's flying very fast.")
Console.WriteLine(Verbs(0)) 'Prints "play"
Console.WriteLine(Verbs(1)) 'Prints "fly"
實施例:http://ideone.com/6TeAmz
'昏暗newTxt =的string.join( 「 」txt.Split(「」 C)。選擇(功能( s)If(s.EndsWith(「ing」),s.Substring(0,s.Length - 3),s))。ToArray())'當然,如果字符串是「國王在戶外玩他的戒指「各種額外的變化發生 – Plutonix