You could do it using the Substring
method, as you say:
For i = 0 To _palabraAAcertar.Length - 1
If tbEscribirLetra.Text = _palabraAAcertar(i) Then
labelResultado.Text =
labelResultado.Text.Substring(0, i) &
_palabraAAcertar(i) &
labelResultado.Text.Substring(i + 1)
End If
Next
The result is the concatenation of the text of the i
characters first, the new character that will replace the line and the following characters to the index i
.
You could also use a StringBuilder
object:
Dim sb As New StringBuilder(labelResultado.Text)
For i = 0 To _palabraAAcertar.Length - 1
If tbEscribirLetra.Text = _palabraAAcertar(i) Then
sb(i) = tbEscribirLetra.Text
End If
Next
labelResultado.Text = sb.ToString()
You create a StringBuilder
object with the initial text, you replace the characters of the indices indicated and you generate the resulting string with the ToString
method.