Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize all strings in an array?

I have a array of strings, but all the stings are written in upper case letters. Is there a way for me to make all the strings in the array to lower case, (and with capitalisation)

array = ["BOY","GIRL","MAN"]  
// func to convert it to array = ["Boy","Girl","Man"]  

Is there a way to do this, without rewriting the content of the array with lower case letters. I have a very long array of strings in upper case letters.

like image 357
Tarjerw Avatar asked Jan 06 '16 11:01

Tarjerw


People also ask

How do you capitalize all strings?

Performing the . upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.

How do you capitalize the first letter in an array of strings?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.

How do you lowercase everything in an array?

To convert all array elements to lowercase:Use the map() method to iterate over the array. On each iteration, call the toLowerCase() method to convert the string to lowercase and return the result. The map method will return a new array containing only lowercase strings.

How do I convert a string array to lowercase?

The toLowerCase method converts a string to lowercase letters. The toLowerCase() method doesn't take in any parameters. Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.


2 Answers

You can use the map function like this:

let array = ["BOY","GIRL","MAN"]

let capitalizedArray = array.map { $0.capitalizedString}
like image 166
totiDev Avatar answered Sep 22 '22 03:09

totiDev


SWIFT 4:

The property capitalizedString has changed to capitalized.

let array = ["BOY","GIRL","MAN"]

let capitalizedArray = array.map {$0.capitalized}
like image 30
Marcy Avatar answered Sep 20 '22 03:09

Marcy