Method to REPLACE characters with NSCharacterSet?

Hello,

I’m trying to find an ASOC way to replace diacriticals characters with their equivalent without diacriticals in an NSString. But I can’t find any method that will help me accomplish that. I know there is a stringByTrimmingCharactersInSet: method, but I do not wish to remove them, I want to switch them.

I had a routine in ASS that worked fine, basically what it did is run through a list of characters and if it hit a match it would replace this character with a character in a replacement list at the same position. That did the trick, but I thought there might be a nice method somewhere that would do that. If nothing exists i’ll update it to be ASOC compatible, but i’d rather use whatever is already there.

I have 2 NSCharacterSet defined, ready to use:

set diacriticalsCharSet to current application's NSCharacterSet's characterSetWithCharactersInString_("áà âäãåçéèêëíìîïñóòôöõúùûüÿÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜŸ")
set replacementCharSet to current application's NSCharacterSet's characterSetWithCharactersInString_("aaaaaaceeeeiiiinooooouuuuyAAAAAACEEEEIIIINOOOOOUUUUY")

I’ve also looked at NSScanner and it does have a method that would do the trick but it requires pointers, so can’t use it in ASOC.

Anyone has a pointer? Or maybe I am looking in the wrong direction?

Thanks!

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

That’s not necessarily so. Are you thinking of scanCharactersFromSet:intoString:?

set {theResult, theString} to theScanner's scanCharctersFromSet_intoString_(theSet, reference)

Have a look at the chapter ‘What Methods Can Return’.

This seemed to work for me when I tried it:

set foreignString to NSString's stringWithString_("áà âäãåçéèêëíìîïñóòôöõúùûüÿÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜŸ")
		log foreignString
		set theData to foreignString's dataUsingEncoding_allowLossyConversion_(current application's NSASCIIStringEncoding, 1)
		set asciiString to NSString's alloc()'s initWithData_encoding_(theData, current application's NSASCIIStringEncoding)
		log asciiString

the result:

Ric

Oooh, interesting… Thanks. I’ll give this one a try for sure! :slight_smile:

Browser: Safari 6533.18.5
Operating System: Mac OS X (10.6)