You can use Enumerable.Reverse
and String.Replace
:
Dim reversed = textBox1.text.Reverse()
label1.Text = New String(reversed.ToArray()).Replace("o", "-").Replace("m", "-")
or this little query which includes both:
Dim newChars = From c In textBox1.Text.Reverse()
Select If({"o", "m"}.Contains(c), "-"c, c)
label1.Text = New String(newChars.ToArray())
or probably even better since it also handles surrogates using the "forgotten" VisualBasic StrReverse
:
label1.Text = Strings.StrReverse(textBox1.Text).Replace("o", "-").Replace("m", "-")
Reversing a String that Contains Unicode Characters Expressed as Surrogate Pairs
Update if you need to replace every second character instead.
Dim newChars = textBox1.Text.Reverse().
Select(Function(c, Index) If(Index Mod 2 = 0, c, "-"c) )
Label1.Text = New String(newChars.ToArray())
Finally here's an approach using a StringBuilder
which replaces every nth character with another character if it's not an "ignore-character" (space in this case):
Public Shared Function ReplaceEveryNthChar(input As String, nth As Int32, newChar As Char, Optional ignoreChars As Char() = Nothing) As String
If input Is Nothing Then
Throw New ArgumentNullException("input")
End If
If input.Length = 0 Then Return ""
If input.Length = 1 Then Return input ' don't replace the first as desired
Dim ignore = New HashSet(Of Char)(If(ignoreChars, Enumerable.Empty(Of Char)()))
Dim sb = New System.Text.StringBuilder(input.Length)
Dim ignoredChars = 0
sb.Append(input(0)) ' don't replace the first as desired
For pos As Int32 = 2 To input.Length
Dim c = input(pos - 1) ' index zero based
If ignore.Contains(c) Then
ignoredChars += 1
sb.Append(c)
ElseIf (pos - ignoredChars) Mod nth = 0 Then
sb.Append(newChar)
Else
sb.Append(c)
End If
Next
Return sb.ToString()
End Function
You use it in this way:
Dim chars = textBox1.Text.ToCharArray()
Array.Reverse(chars)
label1.Text = ReplaceEveryNthChar(New String(chars), 2, "-"c, {" "})