• 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
Parsing strings? File getline?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Parsing strings? File getline?


  • Subject: Parsing strings? File getline?
  • From: email@hidden
  • Date: Tue, 5 Mar 2002 10:18:38 -0800

Sorry for such elementary questions, but I have searched to no avail...

The doc for this is in NSString's main doc file, just so you know. (I believe you did look, I'm not reprimanding you, just telling you where to look...)

Are there no methods for parsing NSStrings? I am looking for something
that would split a string into substrings given a set of separator
characters (perhaps returning an array of string objects). Right now I
am converting the NSString to a C string and using my old favorite
strrtok! Seems lame. I could write a method to do this, but I can't
believe it is not already sitting there.

It is in fact just sitting there. Look up the doc on -[NSString componentsSeparatedByString:]. In general, as soon as a problem even starts to seem like you need to drop down to the standard C library to solve it, you're not looking hard enough at the doc. That may not always true, but it's close -- for newbies, it's pretty much always true.
If your parsing needs become more complex, you might look at NSScanner. But NSString's API suffices for many tasks.

Along the same line, is there no method for grabbing lines of text from
a file?

There was chat on the list about this very topic quite recently, with a simple solution posted. Try searching the archives. Maybe someone can give you a better pointer than that, I didn't pay much attention to the thread. However, I believe the recommended solution for files smaller than, say, a few megabytes, was to read the whole file into an NSString, use that same -componentsSeparatedByString: method to break it at newlines, and then go through the resulting array of lines in a loop.

It seems strange that I am having such a hard time doing this simple
stuff.

Learning how to find one's answers in the doc is half the battle with Cocoa. As much as we all bitch about the state of the doc, it does contain the answers to 99% of the questions asked on this list. :-> But it isn't always easy to find the answers until you get used to how the doc is organized and how the makers of Cocoa thought. (Hmm, makers of Cocoa... maybe I should make myself some hot chocolate... mmm...)

Ben Haller
Stick Software
_______________________________________________
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.

  • Prev by Date: Re: NSUserDefaults question
  • Next by Date: Re: Localizing about files
  • Previous by thread: Re: Parsing strings? File getline?
  • Next by thread: Re: Parsing strings? File getline?
  • Index(es):
    • Date
    • Thread