Tomas
Tomas

Reputation: 18117

Convert string array to lowercase

I have string array string[] MyArray. What is the quickest (meaning least code, not fastest performance) way to convert all string array elements to lowercase?

Upvotes: 36

Views: 68755

Answers (7)

prasadd
prasadd

Reputation: 324

Easiest approach:

MyArray = Array.ConvertAll(MyArray, d => d.ToLower());

Upvotes: 24

SunsetQuest
SunsetQuest

Reputation: 8827

myArray = Array.ConvertAll(myArray, x => x.ToLower());

Note: This is very close to 'prasadd' answer but works with string arrays.

Upvotes: 3

Sergio
Sergio

Reputation: 8259

I wouldn't use this in production:

MyArray = string.Join(";", MyArray).ToLower().Split(';');

Upvotes: -6

SHODAN
SHODAN

Reputation: 1259

I'd go with

var lowercaseStringArray = myStringArray.Select(c => c.ToLower()).ToArray();

Upvotes: 1

Magnus
Magnus

Reputation: 46947

Without creating a new Array.

for (int i = 0; i < MyArray.Length; i++)
    MyArray[i] = MyArray[i].ToLower();

Upvotes: 10

this. __curious_geek
this. __curious_geek

Reputation: 43207

strin[] MyArrayLower = (from str in MyArray
                        select str.ToLower()).ToArray();

Upvotes: 2

jason
jason

Reputation: 241641

var MyArrayLower = MyArray.Select(s => s.ToLowerInvariant()).ToArray();

(or

MyArray = MyArray.Select(s => s.ToLowerInvariant()).ToArray();

if you want to replace the existing array with a new instance of string[].)

Upvotes: 67

Related Questions