|
|
#1 |
|
componentsSeparatedByString
Hello,
I have a string of values, to the effect of: Code:
string = "100 34 154 120 45" Code:
array = [string componentsSeparatedByString:@" "]; 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! |
|
|
|
0
|
|
|
#2 |
|
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]];
}
|
|
|
|
0
|
|
|
#3 |
|
Why not use something like this?
Code:
yourString = @"100 34 154 120 45";
NSMutableString *tempString = [yourString
stringByReplacingOccurrencesOfString:@" "
withString:" "];
tempString = [tempString
stringByReplacingOccurrencesOfString:@" "
withString:" "];
- 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 |
|
|
|
0
|
|
|
#4 |
|
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! |
|
|
|
0
|
|
|
#5 |
|
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 |
|
|
|
0
|
|
|
#6 | |
|
Quote:
![]() 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. |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 08:29 AM.








Linear Mode
