Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

forrestgrant

macrumors member
Original poster
Jun 24, 2008
34
0
Hello,

I have a string of values, to the effect of:
Code:
string = "100  34 154 120  45"
And I use componentsSeparatedByString to give me an array of 5 different values:
Code:
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

Moderator emeritus
Jun 15, 2000
7,958
7
Use NSScanner instead. Something like...

Code:
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

macrumors 6502a
Jul 25, 2001
677
2
Why not use something like this?

Code:
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

macrumors member
Original poster
Jun 24, 2008
34
0
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

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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

macrumors 6502a
Jul 25, 2001
677
2
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.