views:

96

answers:

1

Is there an objective c version of text delimiters??

Here's the applescript code:

set oldDelim to AppleScript's text item delimiters

set AppleScript's text item delimiters to "Jarvis>"
set charliePlainOutput to (what ever needs to be parsed)
set charlieUnmoddedOutput to last text item of charliePlainOutput
set charlieOutputFiltered to text item 1 of charlieUnmoddedOutput
set AppleScript's text item delimiters to "[Jarvis]"
set charlieOutputLast to text item 1 of charlieOutputFiltered
set AppleScript's text item delimiters to "["
set charlieOutput to text item 1 of charlieOutputLast

set AppleScript's text item delimiters to oldDelim

Any ideas?

+2  A: 

The NSString method componentsSeparatedByString: will do basically the same thing as text items in Applescript. You can call than, then access objects from the array to get the substrings. This example grabs all the text after the last occurrence of "Jarvis>" in the original string:

NSString* originalString; //comes from somewhere
NSArray* substrings = [originalString componentsSeparatedByString:@"Jarvis>"];
NSString* lastSubstring = [substrings lastObject];

However, if you just want to grab a substring off the front or the back of an existing string, take a look at rangeOfString:, substringToIndex:, and substringFromIndex:. You would first use rangeOfString: to find the string you want to separate by, then one of the substring methods to grab the actual text you want. This is somewhat more efficient that componentsSeparatedByString: if you only want to access a single substring, rather than every single substring separated by your text delimiter. This example grabs all the text before the first occurrence of "[Jarvis]" in the original string:

NSRange jarvisRange = [originalString rangeOfString:@"[Jarvis]"];
NSString* substring = [originalString substringToIndex:jarvisRange.location];

You can also use rangeOfString:options: passing in NSBackwardsSearch to the options parameter if you want to search from the end of the string rather than the front.

Brian Webster