Re: How to grab everything in a string before a space?
Re: How to grab everything in a string before a space?
- Subject: Re: How to grab everything in a string before a space?
- From: Ondra Cada <email@hidden>
- Date: Tue, 3 Sep 2002 00:26:10 +0200
On Monday, September 2, 2002, at 11:25 , Keith Pritchard wrote:
So tempting to use plain C here ;-)
Perhaps they know what they are doing by deprecating cString ;)))
Have an NSString which looks like "Item1 abcde 12345 y" and all I
need is to get the word before the first space, so, item1 in the
example above.
Whats the easiest way to grab that first word out of the string? Thanks!
The simplest way is
[[string componentsSeparatedByString:@" "] objectAtIndex:0]
In case the string might be longer, it would be too ineffective though, so
you would use
[string substringToIndex:[string rangeOfString:@" "].location];
whose disadvantage is that you either have to know that there would always
be a space, or you should check the range first; the former (ineffective)
method works properly on any string.
Been trying the below but getting a crash -> sigsegv or
incompatible pointer type depending how I play with the definitions.....
NSScanner *myScanner=[NSScanner scannerWithString:myString];
[myScanner scanUpToString:@" " intoString:str1];
Do read the docs -- you need &str1 here, I bet. Nevertheless, for this
task an NSScanner would be an overkill.
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.