JeffK
JeffK

Reputation: 247

NSString make certain words lowercase

I have users check certain phrases for it to build an email. The phrases are separated by a comma but I need the phrases to be lowercase unless it starts with an I or I'm. It works great unless the phrase starts "It" The phrase remains uppercase. I need it to be lowercase.

CODE

    NSString string = [NSString stringWithFormat:@"%@, ",boxesChecked[idx]];
    // strings are in an array
    [boxesChecked enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    if (idx == 0) { //first string

        if ([string rangeOfString:@"I'm"].location != NSNotFound) {
            // leave uppercase
        } else if ([string rangeOfString:@"It"].location != NSNotFound) {
            string = [string stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[string substringToIndex:1] lowercaseString]];

        } else if ([string hasPrefix:@"I"]) {
            // do nothing

        } else {
            string = [string stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[string substringToIndex:1] lowercaseString]];
        }
    } 

Upvotes: 0

Views: 159

Answers (1)

user3634127
user3634127

Reputation: 11

You can change the string to lowe case with this command

NSString *myString = @"Hello, World!"; NSString *lower = [myString lowercaseString]; // this will be "hello, world!"

Upvotes: 1

Related Questions