0

So, I'm having this problem and I have no idea how to handle it Say I have a string with the following format:

"3 6 9 12 13 15 16"

I'm searching for "6" and I find it at position 3, and I remove it. Next, I search for 6 again, and I find it at position IndexOf(6) (whatever that is). This time I don't want to remove it because it's the 6 in 16.

if string1.contains(6) then 
string1 = string1.RemoveAt(string.IndexOf(6),2)
end if

This is vbnet, but any solution to this problem would help.

P.S. This is just a sample code, the main code I'm using has too many things attached to it, and cleaning it for this example would be a nightmare

8
  • See I don't know even basics of VB, but I think you can check the index just before 6 that whether its a white space or some other digit. Similarly check for index just after it. If both are white space, you can remove it. Commented Feb 8, 2015 at 14:44
  • hard to say for sure since that is not the real code and likely not the real string, but you could split the string and examine elements, remove them then re-Join them to create a new string. a variable named string is rather dubious Commented Feb 8, 2015 at 14:44
  • possible duplicate of VB.NET Split string by " " Commented Feb 8, 2015 at 14:46
  • @NewUser sure, that'd work, but i was looking for a fancier way (code/function) that does something similar, instead of doing it manually Commented Feb 8, 2015 at 14:48
  • 1
    it would work because "6" <> "16" Commented Feb 8, 2015 at 14:53

3 Answers 3

1

You asked for a "fancier" solution so I'll give you one:

Dim input As String = "3 6 9 12 13 15 16"
Dim output As String = String.Join(" ", input.Split(" "c).Where(Function(s) s <> "6"))

Debug.WriteLine(output)

3 9 12 13 15 16

Sign up to request clarification or add additional context in comments.

Comments

0

There could be more elegant ways of doing this, but if you are processing numbers, convert it to numbers, then you can just look for the number you are interested in.

If you need it in space delimited form, you can always convert it back. I'm afraid there won't be any "shortcuts" with this one.

Another thing to consider if you are working with actual strings delimited by space, and are looking for patterns, then Regular Expressions is the way to go.

Dim input As String = "3 4 5 6 13 14 15 16"
Dim inputArray() As String = input.Split(" ")
Dim lst As New List(Of Integer)
For Each s In inputArray
     lst.Add(Convert.ToInt32(s))
Next
If lst.Contains(6) Then
    lst.Remove(6)
End If

1 Comment

Thanks. I ended up doing something like this, even before I saw the answer, but I was hoping for a fancier solution. :) Never the less, good job. Thanks again.
0

A better way to solve the problem is to use regex (tested with sed on Mac OSX):

echo "6 3 6 9 12 13 15 16" | sed -E "s/(6 |[^1-9]6| 6$)//g"
# outputs
3 9 12 13 15 16

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.