views:

2527

answers:

4

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:

  • Concatenation
  • Finding the length of a string (especially multi-byte/UTF-8 strings; I do a lot of work with East Asian languages)
  • Pulling just a portion of a string (e.g. the "foobar" out of "abcfoobarxyz")
  • Searching within a string (see the above example)
  • Changing case (upper, lower, title if it's simple to do)
  • Exploding/Imploding strings (e.g. creating and getting information from comma-separated lists)
  • Find/Replace within strings
  • Any other generally useful string functions that might be available
+1  A: 

Those are all written up very clearly in the documentation.

Ken
+11  A: 

There is a String Programming Guide for Cocoa whose table of contents comes awfully close to your list of questions.

Besides this, you might want to look at the documentation of NSString (& NSMutableString) and NSScanner.

f3lix
+2  A: 

In Xcode, press CMD-SHIFT-D and search for NSString.h, NSMutableString and/or NSScanner

Open those files and look at all the things you can do with NSStrings. This should tell you what functions there are.

By the way, this should become second nature to you as you'll be using this a lot to find out what the functions and/or delegates are.

rein
+5  A: 

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 upercaseString]; }

- (NSString*)lowercase:(NSString*)stringToLowercase { return [stringToUppercase lowercaseString]; }

I'm not sure for Titlecase

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

Alexandre Cassagne
Thanks a lot! I've been working my way through the documentation, but it's rather confusing for me at times, whereas dissecting code excerpts to see how it works generally isn't a problem.
Kaji
no problem. Just keep in mind that these are functions (seperate). So you can - and should - use the brackets straight in code (like if you have a hello world program: `- (IBAction)helloButtonClicked:(id)sender { [textField setStringValue:[@"HELLO WORLD" lowercaseString]]; } ` Have fun
Alexandre Cassagne