Amosh Amosh
Amosh Amosh

Reputation: 37

characters replace using vb.net 2012

i have a program that replace ing in some string and its return original verb in that srting like he was playing out door will be he was play out door ...etc

i just want the play without whole string

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

Upvotes: 1

Views: 58

Answers (3)

Visual Vincent
Visual Vincent

Reputation: 18310

The fastest way is to use a one-line Regex.

Dim Output As String = Regex.Match(myInput, "\p{L}+(?=ing[^\p{L}])", RegexOptions.IgnoreCase).Value

A Regex is a class capable of matching strings based on patterns. It's great to use and usually very fast. The pattern is the second string which I've passed to the Match() method. My pattern works like this:

The \p{L}+ part means that it should match every character that is a unicode letter. + means that it should match one or more letters.

The (?=ing[^\p{L}]) part means that the match must end with "ing", and that it's not followed by any unicode letters.


To match multiple verbs we'd have to expand this a bit. Putting it into a function. The function will find all substrings that match the specified pattern, and then put them in a string array and return it to you.

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

Function example usage:

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"

Example: http://ideone.com/6TeAmz

Upvotes: 0

julienduchow
julienduchow

Reputation: 1076

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

Upvotes: 1

Claudius
Claudius

Reputation: 1911

Best way is to replace whole word. You put in word in textbox that you want to add ing to it

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

Upvotes: 0

Related Questions