Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Friday, April 27, 2012
Remove character from NSString in iPhone programming?
NSString *myString = @"A B C D E F G";
I want to remove the spaces, and get a string out like "ABCDEFG".
You could use:
ReplyDeleteNSString *stringWithoutSpaces = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];
If you want to support more than one space at a time, or support any whitespace, you can do this:
ReplyDeleteNSString * noSpaces = [[myString componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: @""];
Taken from NSString
ReplyDeletestringByReplacingOccurrencesOfString:withString:
Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
Parameters
target
The string to replace.
replacement
The string with which to replace target.
Return Value
A new string in which all occurrences of target in the receiver are replaced by replacement.