Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify strings using a "For Each" loop?

       '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, andstring3`. It seems redundant to copy these strings into an array.

like image 757
Vivian River Avatar asked Jul 21 '10 13:07

Vivian River


1 Answers

This example is in C# but the same principle applies.

string[] myStrings = new string[] { "temp", "temp2", "temp3" };
myStrings = myStrings.Select(x => "-" + x + "-").ToArray();

Output:

"-temp-" "-temp2-" "-temp3-"

Link to the MSDN article: http://msdn.microsoft.com/en-us/library/bb548891.aspx

You can also use a delegate function to check whether the string is empty. Will work on providing a VB solution and edit when I get it

EDIT:

Dim myStrings As String() = _
        {"apple", "banana", "mango", Nothing, "passionfruit", "grape"}
    myStrings = _
        myStrings.Select(Function(fruit) ("-" & fruit & "-")).ToArray()

Output:

"-apple-" "-banana-" "-mango-" "--" "-passionfruit-" "-grape-"

EDIT 2: Does the same thing as the first edit but more readable

Private Function TESTER(ByVal fruit As String) As String
    Return "-" & fruit & "-"
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)              Handles MyBase.Load
    Dim myStrings As String() = _
       New String() {"apple", "banana", "mango", Nothing, "passionfruit", "grape"}
    myStrings = _
        myStrings.Select(AddressOf TESTER).ToArray()
End Sub
like image 100
Gage Avatar answered Oct 12 '22 02:10

Gage