Question

I have a string like the number 5 comes after number 3 and number 2 and need to replace the numbers 5,3,2 with 7,2,1.

Using string.replace would lead to this:

after replacing 5 with 7: the number 7 comes after number 3 and number 2

after replacing 3 with 2: the number 7 comes after number 2 and number 2

after replacing 2 with 1: the number 7 comes after number 1 and number 1

the second number is obviously wrong. So I would need to use a placeholder like %firstnumber% %secondnumber% %thirdnumber% and afterwards put the numbers in, but it can be really annoying sicne some strings can have a lot of numbers in it.

What would be a better way to replace the numbers in the string?

I have 3 variables. The first containing the entire old string (the number 5 comes after number 3 and number 2), the second containing the old numbers (532) the third containing the new numbers (721).

I do know how to do it, but I feel that there must be a better way to do it since it doesnt look right to me.

Was it helpful?

Solution

OK... If I understand your scenario correctly what you could do is read a character at a time from source string to a destination string. Each time we do this we check if the character needs to be replaced. Each time we have replaced we use the next char to match.

Dim NumsToMatch As String {"2", "5"}
Dim ReplaceNums as String {"6", "6"}
Dim SourceString as String = "this is 2 my test 5 string"
Dim DestinationStr as New StringBuilder
Dim x = 0

For Each element As Char In SourceString
    If element = NumsToMatch(x) Then
        DestinationStr.Append(ReplaceNums(x))
        x += 1
    Else
        DestinationStr.Append(element)
    End If
Next

Dim OutputStr = DestinationStr.ToString

Output will be "this is 6 my test 6 string"

OTHER TIPS

Don't replace....It would be better if you construct the string up dynamically each time...

Dim Num1 as Integer
Dim Num2 as Integer
Dim Num3 as Integer

Dim OutputStr = "the number " & Num1 & " comes after the number " & Num2 & " and number " & Num3

Then all you need to do is supply your numbers each time you need an new string.

UPDATE

myStr = "The number 5 comes after number 3 and number 2"
myStr.Replace("5", "7", 11, 3)
myStr.Replace("3", "2", 32, 3)
myStr.Replace("2", "1", 45)

Another option would be by using the string's index. If you have a string like

yourStr = "5 is not 2 and 3"

the string's length would be 16, number 5's index/position is in yourStr(0) number 2's index/position is in yourStr(9) number 3's index/position is in yourStr(15)

save the number of indexes into a list or integer array.. whichever you like, and then by looping you can replace each character by a character that you desire

say you have the list now,

mylist(0) = 0 mylist(1) = 9 mylist(2) = 15

For each item as integer in mylist
   if myStr(item) = "0" then
      'mystr.replace()
   elseif myStr(item) = "1" then
      'mystr.replace()
   end if
End For

this is just my idea I know there's another way to do it but yea..

I'm not sure why String.Format() has not been mentioned yet.

String.Format("The number {0} is bigger than {1} is bigger than {2}", 5, 2, 1)
' The result is "The number 5 is bigger than 2 is bigger than 1"

If you already have numbers in your string, and you can't replace the values in your source string, you could do something like this:

  • Current index is zero
  • while non {} wrapped number is in string
    • find the first number
    • replace all occurrences of the first number with "{"+index+"}"
    • increment index
  • ???
  • profit!?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top