Gotten a hold on how to fetch and write to variables in Objective-C, now it's time to learn how to do something more useful with them! Right now, I'm primarily trying to figure out how string manipulation works. In particular, I'm looking for the following functions:
Examples: Concatenation:
- (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 uppercaseString];
}
- (NSString*)lowercase:(NSString*)stringToLowercase
{
return [stringToLowercase lowercaseString];
}
- (NSString*)titlecase:(NSString*)stringToTitleCase
{
return [stringToTitleCase capitalizedString];
}
Find/Replace
- (NSString*)findInString:(NSString*)string
replaceWithString:(NSString*)stringToReplaceWith
{
return [input stringByReplacingOccurrencesOfString:string
withString:stringToReplaceWith];
}
I hope this helps!
PS: Don't forget to check the documentation, and Google is your friend. Good luck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With