Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting the caps in an NSString into lowercase

Tags:

iphone

If you want it all lower case you can do [myString lowercaseString];

If you want it capitalized use [myString capitalizedString];

See Apple's Docs on NSString


 NSString *original = @"blah"
 NSString *lowercase = [original lowercaseString];

This code will make the NSString lowercase be original in all lower case.


lowercaseString

(is there a badge for the fourth answer providing the same solution as the three before?)


If you want to use a specific locale (the NSString docs don't seem to indicate what locale the upper/lowercase/capitalise methods use) then you need to use a CFString function:

NSString* inputString = @"input";
NSString* outputString;
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:@"tr_TR"];

CFMutableStringRef tempCFMString = CFStringCreateMutableCopy(NULL, 0, (CFStringRef)inputString);
CFStringCapitalize(tempCFMString, (CFLocaleRef)locale);
outputString = [[(NSString*)tempCFMString copy] autorelease];
CFRelease(tempCFMString);

//remember to release stuff

[NSString lowercaseString]

Returns a lowercase version of your NSString.


When you want to directly convert the string then,

NSString *str = @"HELLO";
[str lowercaseString];

while you are entering text from textfield then,

[textField.text lowercaseString];

textField is an outlet of UITextField