Friday, 27 September 2013

How to replace one instance of a character in a string and put the output in a text box with the VB language

How to replace one instance of a character in a string and put the output
in a text box with the VB language

We are trying to only replace the first instance of "a" to first second
third. Then it should loop and replace the second instance of "a" to a new
generated value of first second third and continue this loop til there are
no more "a" characters left and each "a" is a different value. However the
results of the output are not correct. The code is erasing all characters
before the last "a" then replacing that last "a".
Dim message As String
Dim start As Integer = 1
Dim letterfinder As String
Dim timesLooped As Integer = 1
Dim length As Integer
Dim first As Integer
Dim second As Integer
Dim third As Integer
Dim count As Integer = 1
message = inputBox.Text
length = Len(message)
timesLooped = 0
While length > timesLooped
Dim random As New Random
letterfinder = Mid$(message, start, 1)
If letterfinder = "a" Then
first = random.Next(3, 9)
second = 1 \ first
third = 1 - second
outputBox.Text = Replace(message, letterfinder, first & second
& third, [start], [count])
End If
timesLooped = timesLooped + 1
start = start + 1
End While

No comments:

Post a Comment