Search code examples
objective-cxcodensstringnscharacterset

Remove Special Character From String


I have a String W

        NSString * stringTitle  = link.title;

where i am getting link.title as @"I_AM_GOOD";

I need to remove the special characters "_" and make is as "I am Good".How to do that?


Solution

  • You haven't defined what you mean by a special character, and you seem to want to replace it with a space, not remove it.

    NSArray * comps = [stringTitle componentsSeparatedByString:@"_"]
    
    NSString * result = nil;
    for(NSString *s in comps)
    {
       if(result)
       {
          result = [result stringByAppendingFormat:@" %@",[s capitalizedString];
       }
       else
       {
           result = [s capitalizedString];
       }
    }
    

    If you have other special characters that you want to replace, then use

    -componentsSeparatedByCharactersInSet: