• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to grab everything in a string before a space?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >How to grab everything in a string before a space? (From: Keith Pritchard <email@hidden>)

  • Prev by Date: NSTextField as a NSToolbarItem
  • Next by Date: NSTextField as a NSToolbarItem (additional info)
  • Previous by thread: How to grab everything in a string before a space?
  • Next by thread: SOAP requests incorrectly formatted when using WebServices framework
  • Index(es):
    • Date
    • Thread