These examples of string manipulation in iPhone apps were taken from the Internet and I don’ pretend on the authority rights for them.
- Appending string to another string:
NSString *myString = @"This";
NSString *test = [myString stringByAppendingString:@" is just a test"];
- Method with wide possibilities to format string and concatenate several strings:
- (NSString*) concatenateString:(NSString*)stringA withString:(NSString*)stringB {
NSString *finalString = [NSString stringWithFormat:@"%@%@", stringA, stringB]; return finalString; }
The advantage of this method is that it is simple to put text between the two strings (e.g. Put a “-” replace %@%@ by %@ – %@ and that will put a dash between stringA and stringB - String Length:
- (int) stringLength:(NSString*)string { return [string length]; //Not sure for east-asian languages, but works fine usually }
- Remove text from string:
- (NSString*)remove:(NSString*)textToRemove fromString:(NSString*)input { return [input stringByReplacingOccurrencesOfString:textToRemove withString:@""]; }
- Uppercase / Lowercase / Titlecase:
- (NSString*)uppercase:(NSString*)stringToUppercase { return [stringToUppercase upercaseString]; }- (NSString*)lowercase:(NSString*)stringToLowercase { return [stringToUppercase lowercaseString]; }
- Find/Replace:
- (NSString*)findInString:(NSString*)string replaceWithString:(NSString*)stringToReplaceWith { return [input stringByReplacingOccurrencesOfString:string withString:stringToReplaceWith]; }
- String comparison Case Sensitive:
if ([category isEqualToString:@"Some String"])
{
doSomething = YES;
}
Case Insensitive:
if (!([category compare:@"Some String" options:NSCaseInsensitiveSearch]))
{
doSomething = YES;
}
References: