3

How would I do this? I'm new to Objective-C but I can't find anything that would help me do this.

NSArray *splitLine = [currentLine componentsSeparatedByString:@":%@",notNumber];

Where notNumber is a string that represents anything that isn't a number. So I want to separate a string where there are colons separated by strings that aren't numbers. (I want to avoid splitting at times i.e. 3:00pm, but split at iCal parameters like DESCRIPTION: and LOCATION:.)

5
  • Does the colon after location and description have a space after it? Commented Jun 30, 2014 at 20:04
  • Unfortunately no! It will always be a string in the alphabet. Commented Jun 30, 2014 at 20:06
  • Okay. Because then you could have done @": " --- Can the time be any time, or is it always going to be on the hour? Commented Jun 30, 2014 at 20:06
  • The data varies. Sometimes it'll say "3pm" which is great, but other times it will say "3:00pm" Commented Jul 2, 2014 at 18:57
  • Ok. you could do something like (this is not code, just logic) if the number before the color's int value is not zero, because i believe letters have an int value of zero, and this may work because its never going to be zero o'clock Commented Jul 2, 2014 at 19:26

3 Answers 3

1

You can do this in several steps, like this. I have not compiled this code, but it should at least give you an idea of what to do.

1) Create a regex object to match your separators:

NSString *regexString = @"DESCRIPTION:\s|LOCATION:\s"; // or whatever makes sense for your scenario
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:regexString
                                          options:NSRegularExpressionCaseInsensitive
                                            error:nil];

2) Replace all the different separators matching your regex with just one separator:

NSRange range = NSMakeRange(0, string.length);
NSString *string2 = [regex stringByReplacingMatchesInString:string
                                                     options:0
                                                       range:range
                                                withTemplate:@"SEPARATOR"];

3) Split the string!

NSArray *elements = [string2 componentsSeparatedByString:@"SEPARATOR"];
Sign up to request clarification or add additional context in comments.

Comments

1

Shortest solution for splitting string.

NSString *str = @"Please split me to form array of words"; 
NSArray *wordsArray  = [str componentsSeparatedByString:@" "];

Comments

0

You can use regular expressions!

Using the pattern (I believe this is the core of your question):

pattern = @"(?<=[^0-9]):(?=[^0-9])"

This pattern will only match ':' symbols not surrounded by numbers.

Then replace with a dummy value that won't show in your data

dummy = @"NEVERSEETHIS"

NSRegularExpressions *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSRange range = NSMakeRange(0, [string length])
NSString *modified= [regex replaceMatchesInString:yourString options:0 range:range withTemplate:dummy];

and finally, split

return [modified componentsSeparatedByString:dummy];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.