Solved

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

Posted on 2015-01-29
7
667 Views
Last Modified: 2016-02-16
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?
0
Comment
Question by:jxbma
7 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 40578742
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.)
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 40578750
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

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40578842
Hi  jxbma;

To your question, "Is there a way I can elegantly convert the entire array to upper case using a LINQ query?", here you go.

Dim isNowUpper() As String = (From s As String In someStrings
                              Select s.ToUpper).ToArray()

Open in new window

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 21

Expert Comment

by:masterpass
ID: 40578872
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 :)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40579030
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

0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 40582052
This is the solution I chose to go with. Thx!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40582056
Not a problem jxbma, glad to help.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question