# Need help on Exact String Search !

• ### Question

• I have 2 textboxes :

`textbox1.text = "*1*4*46*14*144*41*56*67"`

`textbox2.text=""`

I need to look for a number and delete it from the string of textbox1

I wrote this :

``` Dim array1 as string() array1 = Split(TextBox1.Text, "*")

For i As Integer = 0 To UBound(array1)

If array1(i).Contains("14") Then

array1(i) = ""

End If
TextBox2.Text &= array1(i) & vbNewLine

Next

''' Removing empty Lines
Const CRLFCRLF = vbCrLf & vbCrLf

Do While InStr(TextBox2.Text, CRLFCRLF)
TextBox2.Text = Replace(TextBox2.Text, CRLFCRLF, vbCrLf)
Loop

If Strings.Left(TextBox2.Text, 2) = vbCrLf Then TextBox2.Text = Strings.Right(TextBox2.Text, Len(TextBox2.Text) - 2)

If Strings.Right(TextBox2.Text, 2) = vbCrLf Then TextBox2.Text = Strings.Left(TextBox2.Text, Len(TextBox2.Text) - 2)

'** Each number must have "*" preceding it.
TextBox1.Text = "*"

TextBox1.Text &= Strings.Join(TextBox2.Lines, "*")```

What I actually did is that it first splits textbox1 into an array , then It searches for a number like "14". If the number exists, it removes the line containing the number in the array. It puts each array item as a line of textbox2 while doing that. Now it's time to trim textbox2 from empty lines. Finally, it gathers all the numbers with their "*", back into textbox1

The problem is that not only it finds and removes "14", but also it finds and removes "144" !

I want it to only find the number I want , not any other similar numbers too.

** Each number must have "*" preceding it.

• Edited by Monday, February 18, 2019 9:53 AM
Monday, February 18, 2019 9:50 AM

### Answers

• See if this will assist with the current task.

```Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "*1*4*46*14*144*41*56*67"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim findValue As Integer = 0

If Integer.TryParse(TextBox2.Text, findValue) Then

'
' Split string, remove first element as it's empty in this case
' Do a conversion to integer
'
Dim arrIntegers() =
Array.ConvertAll(TextBox1.Text.Split("*"c).ToList().
Where(Function(item) Not String.IsNullOrWhiteSpace(item)).ToArray(),
New Converter(Of String, Integer)(Function(input)
Dim output As Integer = 0

Integer.TryParse(input, output)

Return output
End Function))
'
' Format values
'
TextBox3.Text = String.Join(Environment.NewLine,
arrIntegers.
Where(Function(item) item <> findValue).
Select(Function(item) \$"*{item}").ToArray())

End If

End Sub
End Class
```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

• Marked as answer by Monday, February 18, 2019 11:12 AM
Monday, February 18, 2019 10:52 AM

### All replies

• See if this will assist with the current task.

```Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "*1*4*46*14*144*41*56*67"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim findValue As Integer = 0

If Integer.TryParse(TextBox2.Text, findValue) Then

'
' Split string, remove first element as it's empty in this case
' Do a conversion to integer
'
Dim arrIntegers() =
Array.ConvertAll(TextBox1.Text.Split("*"c).ToList().
Where(Function(item) Not String.IsNullOrWhiteSpace(item)).ToArray(),
New Converter(Of String, Integer)(Function(input)
Dim output As Integer = 0

Integer.TryParse(input, output)

Return output
End Function))
'
' Format values
'
TextBox3.Text = String.Join(Environment.NewLine,
arrIntegers.
Where(Function(item) item <> findValue).
Select(Function(item) \$"*{item}").ToArray())

End If

End Sub
End Class
```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

• Marked as answer by Monday, February 18, 2019 11:12 AM
Monday, February 18, 2019 10:52 AM
• See if this will assist with the current task.

```Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "*1*4*46*14*144*41*56*67"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim findValue As Integer = 0

If Integer.TryParse(TextBox2.Text, findValue) Then

'
' Split string, remove first element as it's empty in this case
' Do a conversion to integer
'
Dim arrIntegers() =
Array.ConvertAll(TextBox1.Text.Split("*"c).ToList().
Where(Function(item) Not String.IsNullOrWhiteSpace(item)).ToArray(),
New Converter(Of String, Integer)(Function(input)
Dim output As Integer = 0

Integer.TryParse(input, output)

Return output
End Function))
'
' Format values
'
TextBox3.Text = String.Join(Environment.NewLine,
arrIntegers.
Where(Function(item) item <> findValue).
Select(Function(item) \$"*{item}").ToArray())

End If

End Sub
End Class
```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

It works well. THANKS!

But I still have a question. Why doesn't method like instr or .contain do the job ? Why don't they add a feature to these searching methods to have for example a Boolean which allows for exact search

It doesn't make sense to me. I have had problems searching in string  with these methods for a long time ...

Monday, February 18, 2019 11:16 AM

• But I still have a question. Why doesn't method like instr or .contain do the job ? Why don't they add a feature to these searching methods to have for example a Boolean which allows for exact search

It doesn't make sense to me. I have had problems searching in string  with these methods for a long time ...

InStr does what it's suppose to do, find a match but does not care about trailing or preceding values as you have found out, this is why the test needs to be done using integer values.

On a side note, have not used InStr since about 1995 because there are better ways to perform these types of task. Some might argue that my solution might have been done with way too much code but it's never about how many lines of code it takes, it about efficiency. A one liner might take 1 second while a three plus line solution may take the same amount of time and the one liner does not work as expected :-)

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

Monday, February 18, 2019 12:11 PM

• But I still have a question. Why doesn't method like instr or .contain do the job ? Why don't they add a feature to these searching methods to have for example a Boolean which allows for exact search

It doesn't make sense to me. I have had problems searching in string  with these methods for a long time ...

InStr does what it's suppose to do, find a match but does not care about trailing or preceding values as you have found out, this is why the test needs to be done using integer values.

On a side note, have not used InStr since about 1995 because there are better ways to perform these types of task. Some might argue that my solution might have been done with way too much code but it's never about how many lines of code it takes, it about efficiency. A one liner might take 1 second while a three plus line solution may take the same amount of time and the one liner does not work as expected :-)

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

I appreciate your knowledge and skills in coding
Monday, February 18, 2019 12:26 PM
• See if this will assist with the current task.

```Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "*1*4*46*14*144*41*56*67"
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim findValue As Integer = 0

If Integer.TryParse(TextBox2.Text, findValue) Then

'
' Split string, remove first element as it's empty in this case
' Do a conversion to integer
'
Dim arrIntegers() =
Array.ConvertAll(TextBox1.Text.Split("*"c).ToList().
Where(Function(item) Not String.IsNullOrWhiteSpace(item)).ToArray(),
New Converter(Of String, Integer)(Function(input)
Dim output As Integer = 0

Integer.TryParse(input, output)

Return output
End Function))
'
' Format values
'
TextBox3.Text = String.Join(Environment.NewLine,
arrIntegers.
Where(Function(item) item <> findValue).
Select(Function(item) \$"*{item}").ToArray())

End If

End Sub
End Class```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

I have got another question about your code.

Can you alter the code to remove only the  existing number at specific position ? For example , the number I want to search is 4 in a string like *5*4*67*32*4*76*4

We now have 3 existing number 4 at different positions : second, fifth and seventh. Imagine I want to remove only the last number 4

I actually want to combine your code with this

• Edited by Thursday, February 21, 2019 8:09 AM
Thursday, February 21, 2019 7:59 AM
• Here you go for removing the last index of a searched value.

```Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim findValue As Integer = 0

If Integer.TryParse(TextBox2.Text, findValue) Then

Dim integers =
Array.ConvertAll(TextBox1.Text.Split("*"c).ToList().
Where(Function(item) Not String.IsNullOrWhiteSpace(item)).ToArray(),
New Converter(Of String, Integer)(Function(input)
Dim output = 0

Integer.TryParse(input, output)

Return output
End Function)).ToList()

'
' do an index on the items
'
Dim values = integers.
Select(Function(data, i) New With {.Value = data, .Index = i}).
ToList()

'
' get last index for value
'
Dim index = values.LastOrDefault(Function(item) item.Value = findValue)

'
' Remove last one
'
If index IsNot Nothing Then
values.RemoveAt(index.Index)
End If

TextBox3.Text = String.
Join(Environment.NewLine, values.Select(Function(xData) xData.Value).
ToArray())

End If
End Sub
End Class
```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

Thursday, February 21, 2019 11:01 AM