I have written a function replacing text within, for example, an array of textboxes.
The function:
Protected Sub Replace(ByVal c1 As Char, ByVal c2 As Char, ByVal ParamArray fields() As TextBox)
For Each field As TextBox In fields
field.Text = field.Text.Replace(c1, c2)
Next
End Sub
Called by these codes (both have the same output):
Replace(" ", String.Empty, _
TextBox1, _
TextBox2 _
)
Replace(" ", "", _
TextBox1, _
TextBox2 _
)
Input: foo bar
Output: foo
Exptected: foobar
Why does it replace all text after the space instead of just the characters I'm giving it?
Option Strict Onto avoid implicit conversions. To make a enclosed quotes char act like a char insted of string use this:" "c" "as the find part of the replace, instead ofc1.c1inside the method, as @JonEgerton said. The replace method takes two strings or two chars, it doesn't allow mixing, so it implicitly convert one of them