Reputation: 7
Happy Friday!
I have a loop that I output the string results to a label, but I want to reverse the order of words...
So instead of the label displaying:
Charlie Beta Alpha
I want it to display:
Alpha Beta Charlie
Code:
For Each CollectionName In CollectionNames
lblResult.Text &= vbCrLf + CStr(CollectionName.Name)
Next
Upvotes: 0
Views: 1699
Reputation: 262
This uses the string function Split and reverses the order before joining them.
Function Reverse(ByVal input As String) As String
Return String.Join(" ", input.Split(" ").Reverse())
End Function
Upvotes: 1
Reputation: 4534
The following function will split a string into words, reverse the words and then build a string with the reversed words.
Function ReverseWords(input As String) As String
Dim words() As String = input.Split(" "c)
Array.Reverse(words)
Return String.Join(" ", words)
End Function
Note that this assumes that the words are originally separated by spaces. If they are separated by something else, you will need to adjust the arguments to the Split
method.
Your comments suggest that you may simply want the reverse the values of CollectionNames
. In that case, you can simply change your loop to this:
For Each CollectionName In CollectionNames
lblResult.Text = CStr(CollectionName.Name) & vbCrLf & lblResult.Text
Next
Upvotes: 0