2016-03-15 45 views
1

我有一個程序,替換荷蘭國際集團在一些字符串,並在srting像其返回原來的動詞他玩戶外他是打出來的門 ...等字符替換用vb.net 2012

我只想發揮沒有整串

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

Dim myInput As String = TextBox1.Text 
Dim myOutput As String = Replace(myInput, "ing", "") 
Label1.Text = myOutput 

End Sub 
+1

'昏暗newTxt =的string.join( 「 」txt.Split(「」 C)。選擇(功能( s)If(s.EndsWith(「ing」),s.Substring(0,s.Length - 3),s))。ToArray())'當然,如果字符串是「國王在戶外玩他的戒指「各種額外的變化發生 – Plutonix

回答

0

最快的方法是使用一個行正則表達式文本框。

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

+0

thanx幫助其接受的答案^ _^ 但如果有超過2個動詞呢?像「他在玩和跑步」?輸出只是「玩」 thanx的幫助 –

+0

@AmoshAmosh:如果有多個動詞,你必須使用一個數組或一串字符串。我會更新我的答案,因爲我也會包括一些解釋。 –

+0

好吧,我會等待更新 和thanx這麼多的幫助 –

0

最好的方法是替換整個單詞。你把字要ING添加到它

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

Dim myInput As String = TextBox1.Text 
Dim myOutput As String = StringToReplacePart.Replace(myInput, String.Format("{0}ing", myInput) 
Label1.Text = myOutput 

End Sub 
+1

但他不想添加」ing「,他想要這個詞_without_」ing「。 –

1
Private Function getVerbOfSetence(ByVal str As String) As String 
    Dim strSpl() As String = str.Split(" ") 
    For i = 0 To strSpl.Length - 1 
     If strSpl(i).ToLower.EndsWith("ing") Then 
      Return strSpl(i).ToLower.Replace("ing", "") 
     End If 
    Next 
    Return "noVerb" 
End Function 
+0

很好的答案,但是你不能代替'ing'。如果動詞是「響鈴」呢?改爲刪除最後三個字符。 –