-2

How in Visual Basics change characters in a string for example (1 to 0) and (0 to 1) without the problem of changing them first to all to 0 and then all to 1, i want to get a result for example like that "00110010101101001010" to "11001101010010110101" (to flip it)

IRONALEKS
  • 50
  • 10

3 Answers3

2

Use an intermediate value.

Change all 0 -> 2
Change all 1 -> 0
Change all 2 -> 1
Greg
  • 414
  • 4
  • 12
1

How about this:

    Dim text = "00110010101101001010"
    Dim flipped = New String(text.Select(Function(c) If(c = "0"c, "1"c, "0"c)).ToArray())

That gives me:

11001101010010110101
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

Another option is to convert the string to a character array iterate over each character individually; then build a new string from the modified array (or overwrite the original):

    Dim data As String = "00110010101101001010"
    Dim arr() As Char = data.ToCharArray
    For i As Integer = 0 To arr.Length - 1
        arr(i) = If(arr(i) = "1", "0", "1")
    Next
    Dim data2 As New String(arr)
    Debug.Print(data)
    Debug.Print(data2)
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40