PDA

View Full Version : componentsSeparatedByString




forrestgrant
Jul 28, 2008, 08:57 AM
Hello,

I have a string of values, to the effect of:
string = "100 34 154 120 45"
And I use componentsSeparatedByString to give me an array of 5 different values:
array = [string componentsSeparatedByString:@" "];
The problem is that some values have one space between them, others have two spaces, some even have three. So I get a very awkward array that is difficult to sort.

I know that I could loop through the array and drop a value if it contains nothing (or just a space) But I have around 250 of these strings to convert to an array and that would slow the process greatly. Is there a way to create the array while just ignoring those extra spaces?

Thanks!



kainjow
Jul 28, 2008, 09:09 AM
Use NSScanner instead. Something like...

NSMutableArray *array = [NSMutableArray array];
NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner setCharactersToBeSkipped:[NSCharacterSet whitespaceCharacterSet]];
while (![scanner isAtEnd]) {
int num;
[scanner scanInt:&num];
[array addObject:[NSNumber numberWithInt:num]];
}

DaveGee
Jul 28, 2008, 09:16 AM
Why not use something like this?


yourString = @"100 34 154 120 45";

NSMutableString *tempString = [yourString
stringByReplacingOccurrencesOfString:@" "
withString:" "];

tempString = [tempString
stringByReplacingOccurrencesOfString:@" "
withString:" "];


Notes:
- The 1st line has 3 spaces in the "replace..." and 1 space in the withString
- The 2nd line has 2 spaces in the "replace..." and 1 space in the withString
- You could add any number of additional statements to replace what you don't want with what you do...

The final 'good string' will be in tempString which is just a temp variable (will not be retained) so you need to make use of it and be done with it cause it will go away as your program continues to run...

- Also notice that the first time I use stringByReplacingOccurrencesOfString I create a temp NSMutableString (using yourString that holds the actual string) and then the second time I do the stringByReplacingOccurrencesOfString I write the new result right over the top of the old NSMutableString.

Hope this makes some sense... Oh and this might not be the most perfect or efficient way but its one that came to me quickly...

Dave

forrestgrant
Jul 28, 2008, 09:34 AM
Yeah, that makes total sense. I had that idea, originally discarded it because it is somewhat unknown how many spaces there are. But I suppose doing that process for 4, 3, 2 spaces is better than looping through a bad array and dropping blank data.

Thanks!

Sbrocket
Jul 28, 2008, 11:27 AM
Yeah, that makes total sense. I had that idea, originally discarded it because it is somewhat unknown how many spaces there are. But I suppose doing that process for 4, 3, 2 spaces is better than looping through a bad array and dropping blank data.

Thanks!

I'm not sure how much you care, but kainjow's solution really is the "better" one especially if you can't place limits on the number of spaces that might be in the string. I mean, both solutions could work, but using NSScanner is the most scalable.

DaveGee
Jul 29, 2008, 07:51 AM
I'm not sure how much you care, but kainjow's solution really is the "better" one especially if you can't place limits on the number of spaces that might be in the string. I mean, both solutions could work, but using NSScanner is the most scalable.

It we had a real (or any kind of) regex I could come up with a 'way better' and totally **UN-understandable** solution.. Curses on you Apple curses....

:D

Being less silly, I'd still love to see regex in cocoa - I think we are the only formal or even semi-formal language that doesn't have some easy access to it. Kinda sad if you think about it, so I try not to but then issues like this come up and a regex just pops up in an instant and elegant (but less than easy to understand) one line statement that would take care of the OPs request in a heartbeat (heck it can do super complex one liners in the flash of an eye)... What the op was requesting is about as basic as it gets I'd bet the regex command to do what was needed would be what 6 chars long? probably less... it would be so cool to do stuff that regex allows you to do with an NSString. Dream....

Dave