Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 28, 2008, 08:57 AM   #1
forrestgrant
macrumors member
 
Join Date: Jun 2008
componentsSeparatedByString

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!
forrestgrant is offline   0 Reply With Quote
Old Jul 28, 2008, 09:09 AM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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]];
}
kainjow is offline   0 Reply With Quote
Old Jul 28, 2008, 09:16 AM   #3
DaveGee
macrumors 6502a
 
Join Date: Jul 2001
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
DaveGee is offline   0 Reply With Quote
Old Jul 28, 2008, 09:34 AM   #4
forrestgrant
Thread Starter
macrumors member
 
Join Date: Jun 2008
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!
forrestgrant is offline   0 Reply With Quote
Old Jul 28, 2008, 11:27 AM   #5
Sbrocket
macrumors 65816
 
Sbrocket's Avatar
 
Join Date: Jun 2007
Location: /dev/null
Quote:
Originally Posted by forrestgrant View Post
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.
__________________

Unibody MBP 15" | 2.53GHz | 4GB DDR3 | 320GB @ 7200RPM
Sbrocket is offline   0 Reply With Quote
Old Jul 29, 2008, 07:51 AM   #6
DaveGee
macrumors 6502a
 
Join Date: Jul 2001
Quote:
Originally Posted by Sbrocket View Post
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....



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

Last edited by DaveGee; Jul 29, 2008 at 07:59 AM.
DaveGee is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 10:26 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC