Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the str.translate() method from Python in objective c?

So the title explains most of it. I'm starting to work on Objective c for iOS and I haven't found out if there is a way to use a translate()-like in Objective c.

This is the program I've used for it in python.:

#!/usr/bin/python

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab);

Output:

th3s 3s str3ng 2x1mpl2....w4w!!!

like image 673
coldblade2000 Avatar asked Dec 08 '25 13:12

coldblade2000


1 Answers

As far as I'm concerned, there is no builtin method for like translate(). (You could, however, get the exact same function in Objective C by using PyObjc, look it up)

You could try doing something with replaceOccurrencesOfString:withString:options:range on a NSMutableString or write a function yourself, with a loop that looks at every character in string, checks if it has to be replaced, and if so, replaces it with the right character. (Because that is what the translate() function does, right?)

like image 172
rien333 Avatar answered Dec 10 '25 02:12

rien333