'Why doesn't this work? Dim myStrings As String() = New String() {string1, string2, string3,} For Each s As String In myStrings If String.IsNullOrEmpty(s) Then s = "" End If s = "-" & s.Trim() & "-" Next
If string1
contains "foo"
, my intention is that string1
contains "-foo-"
after the loop executes. How can I make this work?
I'm guessing that this code makes copies of my strings and modifies those. How can I modify my strings in a loop?
Update I've modified the code to use array indexes:
' It still doesn't work. Dim myStrings As String() = New String() {string1, string2, string3} For i As Integer = 0 To myStrings.Count() - 1 If String.IsNullOrEmpty(myStringss(i)) Then myStringss(i) = "" End If myStrings(i) = "-" & myStrings(i) & "-" Next
The result of this code, referring specifically to the array and index for each element, modifies the array, but my strings still have the same old values. Apparently, initializing an array like this simply copies my values into a new array. How can I modify my original values in a loop?
Emphasis: The array is just for looping purposes. I need to modify string1
, string2
, and string3
similarly, but once this loop is thru, there is no more use for the array. Oh, and my real code has more than 3 strings.
Let me just say that if I were using a language with more pointers and references, I think I would simply have an array of pointers that point to string1
, string2, and
string3`. It seems redundant to copy these strings into an array.