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.
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.
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.
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.
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.
You can use the map function like this:
let array = ["BOY","GIRL","MAN"]
let capitalizedArray = array.map { $0.capitalizedString}
SWIFT 4:
The property capitalizedString has changed to capitalized.
let array = ["BOY","GIRL","MAN"]
let capitalizedArray = array.map {$0.capitalized}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With