• 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: Clever way to replace in a NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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] initWithData:[olds dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding] autorelease];

Also best to be put into a category, like

-(NSString*)stringByRemovingAccents {
return [[[NSString alloc] initWithData:[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.


References: 
 >Clever way to replace in a NSString (From: "Mathieu Godart" <email@hidden>)

  • Prev by Date: Re: Getting glyphs and their outlines
  • Next by Date: Re: Completely resizing NSImage
  • Previous by thread: Re: Clever way to replace in a NSString
  • Next by thread: Subclassing NSTabView & handling mouseDragged messages
  • Index(es):
    • Date
    • Thread