I am trying to capitalize the first letter string sent
in the array arr
. The part of the code that does not work is the Right
function, which causes the code to return an error. What could be the fix?
For Each sent In arr
sent = UCase(Left(sent, 1)) & Right(sent, Len(sent) - 1)
arr(i) = sent
i = i + 1
Next
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. Now, we would get the remaining characters of the string using the slice() function.
In JavaScript, we have a method called toUpperCase() , which we can call on strings, or words. As we can imply from the name, you call it on a string/word, and it is going to return the same thing but as an uppercase.
The String type is capitalized because it is a class, like Object , not a primitive type like boolean or int (the other types you probably ran across).
The first letter of a string can be capitalized using the capitalize() function. This method returns a string with the first letter capitalized. If you are looking to capitalize the first letter of the entire string the title() function should be used.
You can just use the StrConv()
function for this. e.g. :
For i = LBound(arr) To UBound(arr)
sent = arr(i)
arr(i) = StrConv(sent, vbProperCase)
Next
or without a loop:
arr = Split(StrConv(Join$(arr, " "), vbProperCase), " ")
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