所以标题解释了大部分内容。我开始为 iOS 开发 Objective c,但我还没有发现是否可以在 Objective c 中使用类似 translate() 的方法。
这是我在 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);
输出:
th3s 3s str3ng 2x1mpl2....w4w!!!
最佳答案
就我而言,没有像 translate()
这样的内置方法。 (但是,您可以使用 PyObjc 在 Objective C 中获得完全相同的功能,请查找)
你可以尝试用 replaceOccurrencesOfString:withString:options:range
做点什么在 NSMutableString 上或自己编写一个函数,使用一个循环查看字符串中的每个字符,检查是否必须替换它,如果是,则用正确的字符替换它。 (因为这就是 translate()
函数的作用,对吧?)
关于python - 如何在 objective-c 中使用 Python 中的 str.translate() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12763253/