FROM : Jonathan E. Jackel
DATE : Fri Nov 22 17:04:22 2002
> I can split a string in smaller ones using componentsSeparatedByString:
>
> - (NSArray *)componentsSeparatedByString:(NSString *)separator
>
> Where separator is the string at which the main string should be
> broken. Is it possible to test for more than one separators at a
> time, e.g. split the string if the separator is either a space or tab
> (or any other user specified character(s)?
I can think of two ways to do this:
1. If you are splitting a mutable string, first use
replaceOccurrencesOfString:withString:options:range: to change all of the
separators into the same separator character. Then use
componentsSeparatedByString: to parse the string.
2. Use an NSScanner, which can look for sets of characters. You can define
your own set of characters to scan for. Scanner code is quite ugly and
verbose compared to componentsSeparatedByString:, but it gets the job done.
http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/Programmin
gTopics/Strings/Concepts/Scanners.html
>
> Maybe I should just first test for the first separator on the main
> string, and then search for the second one in all the smaller strings?
No, then you'll get nested arrays. This is fine if you want to divide a
string into records, and then the records into fields. It sounds like your
string is a single record, however, so you want to end up with one array.
Jonathan
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
DATE : Fri Nov 22 17:04:22 2002
> I can split a string in smaller ones using componentsSeparatedByString:
>
> - (NSArray *)componentsSeparatedByString:(NSString *)separator
>
> Where separator is the string at which the main string should be
> broken. Is it possible to test for more than one separators at a
> time, e.g. split the string if the separator is either a space or tab
> (or any other user specified character(s)?
I can think of two ways to do this:
1. If you are splitting a mutable string, first use
replaceOccurrencesOfString:withString:options:range: to change all of the
separators into the same separator character. Then use
componentsSeparatedByString: to parse the string.
2. Use an NSScanner, which can look for sets of characters. You can define
your own set of characters to scan for. Scanner code is quite ugly and
verbose compared to componentsSeparatedByString:, but it gets the job done.
http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/Programmin
gTopics/Strings/Concepts/Scanners.html
>
> Maybe I should just first test for the first separator on the main
> string, and then search for the second one in all the smaller strings?
No, then you'll get nested arrays. This is fine if you want to divide a
string into records, and then the records into fields. It sounds like your
string is a single record, however, so you want to end up with one array.
Jonathan
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
| Related mails | Author | Date |
|---|---|---|
| Koen van der Drift | Nov 22, 15:06 | |
| Jonathan E. Jackel | Nov 22, 17:04 | |
| Koen van der Drift | Nov 22, 17:18 | |
| Koen van der Drift | Nov 23, 18:34 | |
| Koen van der Drift | Nov 24, 01:12 | |
| Jonathan Jackel | Nov 25, 00:04 | |
| Koen van der Drift | Nov 25, 02:37 | |
| Koen van der Drift | Nov 25, 03:03 | |
| Jonathan Jackel | Nov 25, 04:12 | |
| Koen van der Drift | Nov 25, 14:54 | |
| Koen van der Drift | Nov 29, 20:02 | |
| Jonathan Jackel | Nov 30, 04:06 |






Cocoa mail archive

