Link to home
Start Free TrialLog in
Avatar of jxbma
jxbmaFlag for United States of America

asked on

How do I convert an array of String to UpperCase in VB.net?

Hi:

I have an array of strings declared in vb.net

Dim someStrings() as String

Open in new window


I now I could do a for loop and convert each member of the array to uppercase.

Is there a way I can elegantly convert the entire array to upper case using a LINQ query?
Avatar of rspahitz
rspahitz
Flag of United States of America image

How about something like this (not using LINQ):

Dim strAll  As String = Join(someStrings, "~")
strAll.toUpperCase()
someStrings = strAll.split("~")

(I'm not sure if the syntax is quite right, but it should be close, and could probably all be done in one line.)
Here's a VBA version that works, although there are improvements in VB.Net (which I can check when I get home if the above doesn't quite work):

Sub x()
    Dim someStrings() As String
    ReDim someStrings(3)
    someStrings(0) = "kljdsklgjjkenrgh"
    someStrings(1) = ".dklsjgkldjklgdf"
    someStrings(2) = ".dsjfkldfsjkldfs"
    someStrings(3) = ". kldjfkldjs"
    Dim strAll As String
    strAll = UCase(Join(someStrings, "~"))
    someStrings = Split(strAll, "~")
    MsgBox someStrings(3)
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or something like this:

Dim someStrings() As String = {"aaa", "bbb", "ccc"}
Dim upperArray = someStrings.Select(Function(s) s.ToUpper()).ToArray()

Open in new window


HTH :)
Here's a clean non-LINQ approach:
        Dim someStrings() As String = {"cat", "dog", "fish"}
        someStrings = Array.ConvertAll(Of String, String)(someStrings, Function(s) s.ToUpper)

Open in new window

Avatar of jxbma

ASKER

This is the solution I chose to go with. Thx!
Not a problem jxbma, glad to help.