Re: trouble getting c-chars from NSString
Re: trouble getting c-chars from NSString
- Subject: Re: trouble getting c-chars from NSString
- From: Jim Hamilton <email@hidden>
- Date: Thu, 31 Mar 2005 10:33:28 -0500
On Mar 31, 2005, at 9:21 AM, M. Uli Kusterer wrote:
At 16:48 Uhr -1000 30.03.2005, Daniel Child wrote:
Sorry, I am a newbie and I only did a tiny bit of C, so this question is pretty basic. I am trying to convert an NSString to a C-String so that I can remove certain characters. Somehow I don't seem to be able to get the cString returned from the [NSString cString] method to be assigned to a character array.
You're aware that you don't need to use a C string, right? You can just use an NSMutableString and characterAtIndex: to do the work. That will also take care of international characters that might not fit in a C string, which you'd otherwise lose.
This is true as far as it goes; it just doesn't go far enough. Remember that NS(Mutable)String encodes characters in UTF-16. A "character" in an NSString might not be what one would expect, because of combining characters and surrogate pairs. [While it's probably more information than you want, you can look at <http://unicode.org/faq/>.]
To the OP: Look at NSCharacterSet. In fact, there was recently a thread on "internally trimming" characters from a string.
On Mar 31, 2005, at 12:41 AM, Christopher Drum wrote:
Is there an easy way to remove characters belong to a set (like numbers) from anywhere in a string? stringByTrimmingCharacters only works on the ends of the string.
This is the easiest way I know of, personally...
NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
NSMutableString *originalWord = [NSMutableString stringWithString:@"F1o2o3b4a5r"];
NSRange r = [originalWord rangeOfCharacterFromSet:digits];
while (r.location != NSNotFound) {
[originalWord deleteCharactersInRange:r];
r = [originalWord rangeOfCharacterFromSet:digits];
}
I'm not sure, but I think this does exactly what you want. That said, please also look at what others have posted on this thread, and also please delve into the documentation for NSCharacterSet.
Jim H
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden