I am having a NSString to which I am trimming at the start and end for blank spaces. Now I want to make the first alphabet or letter of the string to appear as capital.
Is there any short available or I need to take the first alphabet and convert it to capital on my own.
tnx.
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.
Just use the capitalizedString method. A string with the first character from each word in the receiver changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values.
To convert a String to Uppercase in Swift, use String. uppercased() function.
You have to do it by hand. This is one of those things that has been built into categories so often by so many people that Apple probably doesn't dare add it as an actual method since it would generate category collisions.
NSString *firstCapChar = [[string substringToIndex:1] capitalizedString];
NSString *cappedString = [string stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
There are of course many other solutions using NSMutableString or any other way you like to do it using unichar or whatever.
- (NSString *)capitalizedString
will make all words in the string initially capitalized. If you only have one or two words in your string, this may be what you want.
Here is the NSString class reference.
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