Re: Clever way to replace in a NSString
Re: Clever way to replace in a NSString
- Subject: Re: Clever way to replace in a NSString
- From: Ondra Cada <email@hidden>
- Date: Mon, 29 Mar 2004 21:50:25 +0200
Mathieu,
On Monday, Mar 29, 2004, at 15:30 Europe/Prague, Mathieu Godart wrote:
I'm wondering if there is an easy and efficient way to replace a
specific
character by an other in a NSString (let's say I want to replace all
spaces
by '+' char).
Not extremely efficient perhaps, but convenient as hell:
news=[[olds componentsSeparatedByString:@" "]
componentsJoinedByString:@"+"];
Presumed you do that often, make a category method with the above
implementation, and improve the implementation when and if needed.
I'm more a C programer than Cocoa developper (for the moment ;-) ),
for that
reason, the best way I can see is converting the string in C and
manipulate
it with my good old C functions.
Don't: Unicode would go poof. Also it *might* be *very* inefficient
(depending on the way the original string happened to be stored).
The second funny thing I want to do with NSString is to relace all
characters with accents ('i', 'h', 'o', 'g', etc.) by their equivalent
without accent ('e, 'e', 'i', 'c', etc.).
news=[[[NSString alloc] initWith
Data:[olds
dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]
encoding:NSASCIIStringEncoding] autorelease];
Also best to be put into a category, like
-(NSString*)stringByRemovingAccents {
return [[[NSString alloc] initWith
Data:[self
dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]
encoding:NSASCIIStringEncoding] autorelease];
}
One laste thing, is it possible to extract a substring matching a reg
exp?
Not in pure Cocoa, it does not support regexps at all. There are 3rd
party kits though; try eg. MOKit.
---
Ondra Hada
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.