Chris JJ
Chris JJ

Reputation: 11

VB.net removing and keeping words in a textbox

[{"name":"chrisjj","uuid":"d086112c-6e25-31a0-acf0-f95c3ca98784","expiresOn":"2016-02-22 23:04:35 +0000"}]

[{"name":"ben","uuid":"d086112c-7a26-33b5-ucf3-j96c1ca26854","expiresOn":"2015-011-12 22:04:35 +0000"}]

Basically im working on a project for a while now and I am trying to keep the names chrisjj and ben and removing the rest of the text from textbox in visual basic 2012 if you have any idea that would be great help

Upvotes: 1

Views: 444

Answers (2)

Juanche
Juanche

Reputation: 104

You can do this:

If InStr(Textbox1.Text, "chrisjj") Then
Textbox1.text = "chrisjj"
else if InStr(Textbox1.Text, "ben") Then
Textbox1.text = "ben"
end if

The InStr Function returns the position of the first occurrence of one string within another.

Also

if TextBox1.Text.Contains("chrisjj") Then
TextBox1.Text = TextBox1.Text = "chrisjj"
ElseIf TextBox2.Text.Contains("ben") Then
TextBox1.Text = TextBox1.Text = ben
end if

The String.Contains Method (String) returns a value indicating whether a specified substring occurs within this string.

Upvotes: 0

Youssef13
Youssef13

Reputation: 4954

You may use regex to achieve what you want.

Dim Input As String = RichTextBox1.Text
Dim MC As MatchCollection = Regex.Matches(Input, Regex.Escape("[{""name"":""") & "[chrisjj|ben].*?" & Regex.Escape("]"), RegexOptions.IgnoreCase)
Dim Output As New List(Of String)

For i = 0 To MC.Count - 1
    Output.Add(MC(i).Value)
Next
MsgBox(String.Join(vbNewLine, Output.ToArray()))

I think this is what you want. this regex matches [{"name":" then chrisjj or ben and goes on until ] is found.

Upvotes: 1

Related Questions