如何将字符串中的英文数字转换为 Objective-C 中的 Persian/Arabic 数字?
How to convert english numbers inside a string to Persian/Arabic numbers in Objective-C?
我有一个英文字符串,可能有也可能没有数字。但我希望这些数字作为波斯数字打印在屏幕上。
例如如果NSString *foo = @"a string with numbers 1 2 3;"
那么输出应该是a string with numbers ۱۲۳
我现在使用的代码是:
-(NSString *)convertEnNumberToFarsi:(NSString *) number{
NSString *text;
NSDecimalNumber *someNumber = [NSDecimalNumber decimalNumberWithString:number];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fa"];
[formatter setLocale:gbLocale];
text = [formatter stringFromNumber:someNumber];
return text;
}
此方法仅转换仅包含数字的字符串,但如上所述,我想转换任何可能包含或不包含数字的字符串。
我怎样才能做到这一点?
恐怕没有别的办法了,只能更换了。
例如:
str =[str stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
.
.
.
简单的方法:
NSDictionary *numbersDictionary = @{@"1" : @"۱", @"2" : @"۲", @"3" : @"۳", @"4" : @"۴", @"5" : @"۵", @"6" : @"۶", @"7" : @"۷", @"8" : @"۸", @"9" : @"۹",@"0" : @"٠"};
for (NSString *key in numbersDictionary) {
str = [str stringByReplacingOccurrencesOfString:key withString:numbersDictionary[key]];
}
其他更灵活的语言环境解决方案:
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"ar"];
for (NSInteger i = 0; i < 10; i++) {
NSNumber *num = @(i);
str = [str stringByReplacingOccurrencesOfString:num.stringValue withString:[formatter stringFromNumber:num]];
}
注意:这段代码没有IDE,可能有语法错误。
在swift 3:
func convertEngNumToPersianNum(num: String)->String{
let number = NSNumber(value: Int(num)!)
let format = NumberFormatter()
format.locale = Locale(identifier: "fa_IR")
let faNumber = format.string(from: number)
return faNumber!
}
在强制展开之前检查以防止崩溃。
+(NSString *)convertNumToPersian:(NSString *)EnglishNumString
{
if ( [EnglishNumString isEqual:[NSNull null]] )
return @"";
NSString *myString = [EnglishNumString stringByReplacingOccurrencesOfString:@"1" withString:@"۱"];
myString = [myString stringByReplacingOccurrencesOfString:@"2" withString:@"۲"];
myString =[myString stringByReplacingOccurrencesOfString:@"3" withString:@"۳"];
myString =[myString stringByReplacingOccurrencesOfString:@"4" withString:@"۴"];
myString =[myString stringByReplacingOccurrencesOfString:@"5" withString:@"۵"];
myString =[myString stringByReplacingOccurrencesOfString:@"6" withString:@"۶"];
myString =[myString stringByReplacingOccurrencesOfString:@"7" withString:@"۷"];
myString =[myString stringByReplacingOccurrencesOfString:@"8" withString:@"۸"];
myString =[myString stringByReplacingOccurrencesOfString:@"9" withString:@"۹"];
myString =[myString stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
return myString;
}
Swift 3:
不过其他回答都对,就是String
转Int
有点问题。
将 String
转换为 Int
会删除左边的零,这不好(特别是在转换手机和国家 ID 时)。为了避免这个问题,我更喜欢将每个英语数字替换为波斯语。
static func convertToPersian(text : String)-> String {
let numbersDictionary : Dictionary = ["0" : "۰","1" : "۱", "2" : "۲", "3" : "۳", "4" : "۴", "5" : "۵", "6" : "۶", "7" : "۷", "8" : "۸", "9" : "۹"]
var str : String = text
for (key,value) in numbersDictionary {
str = str.replacingOccurrences(of: key, with: value)
}
return str
}
此方法也可用于替换文本中的数字,无需将纯数字转换为波斯语。
/**
* 在此函数中将阿拉伯数字转换为英文数字。
* @sample stringValue : 传递字符串值。
*/
fun numberArabicToEnglish(stringValue: String): String {
val value = stringValue
.replace("١", "1")
.replace("٢", "2")
.replace("٣", "3")
.replace("٤", "4")
.replace("٥", "5")
.replace("٦", "6")
.replace("٧", "7")
.replace("٨", "8")
.replace("٩", "9")
.replace("٠", "0")
return value
}`enter code here`
将数字转换为任何语言环境
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "ar")
if let localized = formatter.string(from: NSNumber(value: comp.0)) {
return "\(localized)"
}
示例:
(lldb) po formatter.string(from: NSNumber(value: 0)) ▿
Optional
- some : "٠"
(lldb) po formatter.string(from: NSNumber(value: 1)) ▿
Optional
- some : "١"
(lldb) po formatter.string(from: NSNumber(value: 2)) ▿
Optional
- some : "٢"
(lldb) po formatter.string(from: NSNumber(value: 3)) ▿
Optional
- some : "٣"
(lldb) po formatter.string(from: NSNumber(value: 4)) ▿
Optional
- some : "٤"
(lldb) po formatter.string(from: NSNumber(value: 5)) ▿
Optional
- some : "٥"
(lldb) po formatter.string(from: NSNumber(value: 6)) ▿
Optional
- some : "٦"
(lldb) po formatter.string(from: NSNumber(value: 7)) ▿
Optional
- some : "٧"
(lldb) po formatter.string(from: NSNumber(value: 8)) ▿
Optional
- some : "٨"
(lldb) po formatter.string(from: NSNumber(value: 9)) ▿
Optional
- some : "٩"
(lldb) po formatter.string(from: NSNumber(value: 10)) ▿
Optional
- some : "١٠"
Swift 5 及以上
如果你想创建 UILabel 的子类:
class LocalizedNumbersLabel: UILabel {
override var text: String? {
didSet {
if let text = text {
let numbersDictionary : Dictionary = ["0" : "۰","1" : "۱", "2" : "۲", "3" : "۳", "4" : "۴", "5" : "۵", "6" : "۶", "7" : "۷", "8" : "۸", "9" : "۹", "." : ","]
var str = text
for (key,value) in numbersDictionary {
str = str.replacingOccurrences(of: key, with: value)
}
if(str != self.text){
self.text = str
}
} else {
}
}
}
}
在swift5
func getNumerals(num: Int) -> String {
if getLanguage() == "ar" // You can set locale of your language
{
let number = NSNumber(value: num)
let format = NumberFormatter()
format.locale = Locale(identifier: "ar") // You can set locale of your language
let formatedNumber = format.string(from: number)
return formatedNumber!
} else {
return "\(num)"
}
}
从任何语言到任何语言
您可以将任何语言的数字转换成任何其他语言,例如:
"سلام 12345".convertedDigitsToLocale(Locale(identifier: "FA")) /* سلام ۱۲۳۴۵ */
"Hello ۱۲۳۴۵".convertedDigitsToLocale(Locale(identifier: "EN")) /* Hello 12345 */
分机:
具有以下扩展名:
extension String {
private static let formatter = NumberFormatter()
func clippingCharacters(in characterSet: CharacterSet) -> String {
components(separatedBy: characterSet).joined()
}
func convertedDigitsToLocale(_ locale: Locale = .current) -> String {
let digits = Set(clippingCharacters(in: CharacterSet.decimalDigits.inverted))
guard !digits.isEmpty else { return self }
Self.formatter.locale = locale
let maps: [(original: String, converted: String)] = digits.map {
let original = String([=11=])
guard let digit = Self.formatter.number(from: String([=11=])) else {
assertionFailure("Can not convert to number from: \(original)")
return (original, original)
}
guard let localized = Self.formatter.string(from: digit) else {
assertionFailure("Can not convert to string from: \(digit)")
return (original, original)
}
return (original, localized)
}
var converted = self
for map in maps { converted = converted.replacingOccurrences(of: map.original, with: map.converted) }
return converted
}
}
我有一个英文字符串,可能有也可能没有数字。但我希望这些数字作为波斯数字打印在屏幕上。
例如如果NSString *foo = @"a string with numbers 1 2 3;"
那么输出应该是a string with numbers ۱۲۳
我现在使用的代码是:
-(NSString *)convertEnNumberToFarsi:(NSString *) number{
NSString *text;
NSDecimalNumber *someNumber = [NSDecimalNumber decimalNumberWithString:number];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fa"];
[formatter setLocale:gbLocale];
text = [formatter stringFromNumber:someNumber];
return text;
}
此方法仅转换仅包含数字的字符串,但如上所述,我想转换任何可能包含或不包含数字的字符串。
我怎样才能做到这一点?
恐怕没有别的办法了,只能更换了。
例如:
str =[str stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
.
.
.
简单的方法:
NSDictionary *numbersDictionary = @{@"1" : @"۱", @"2" : @"۲", @"3" : @"۳", @"4" : @"۴", @"5" : @"۵", @"6" : @"۶", @"7" : @"۷", @"8" : @"۸", @"9" : @"۹",@"0" : @"٠"};
for (NSString *key in numbersDictionary) {
str = [str stringByReplacingOccurrencesOfString:key withString:numbersDictionary[key]];
}
其他更灵活的语言环境解决方案:
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"ar"];
for (NSInteger i = 0; i < 10; i++) {
NSNumber *num = @(i);
str = [str stringByReplacingOccurrencesOfString:num.stringValue withString:[formatter stringFromNumber:num]];
}
注意:这段代码没有IDE,可能有语法错误。
在swift 3:
func convertEngNumToPersianNum(num: String)->String{
let number = NSNumber(value: Int(num)!)
let format = NumberFormatter()
format.locale = Locale(identifier: "fa_IR")
let faNumber = format.string(from: number)
return faNumber!
}
在强制展开之前检查以防止崩溃。
+(NSString *)convertNumToPersian:(NSString *)EnglishNumString
{
if ( [EnglishNumString isEqual:[NSNull null]] )
return @"";
NSString *myString = [EnglishNumString stringByReplacingOccurrencesOfString:@"1" withString:@"۱"];
myString = [myString stringByReplacingOccurrencesOfString:@"2" withString:@"۲"];
myString =[myString stringByReplacingOccurrencesOfString:@"3" withString:@"۳"];
myString =[myString stringByReplacingOccurrencesOfString:@"4" withString:@"۴"];
myString =[myString stringByReplacingOccurrencesOfString:@"5" withString:@"۵"];
myString =[myString stringByReplacingOccurrencesOfString:@"6" withString:@"۶"];
myString =[myString stringByReplacingOccurrencesOfString:@"7" withString:@"۷"];
myString =[myString stringByReplacingOccurrencesOfString:@"8" withString:@"۸"];
myString =[myString stringByReplacingOccurrencesOfString:@"9" withString:@"۹"];
myString =[myString stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
return myString;
}
Swift 3:
不过其他回答都对,就是String
转Int
有点问题。
将 String
转换为 Int
会删除左边的零,这不好(特别是在转换手机和国家 ID 时)。为了避免这个问题,我更喜欢将每个英语数字替换为波斯语。
static func convertToPersian(text : String)-> String {
let numbersDictionary : Dictionary = ["0" : "۰","1" : "۱", "2" : "۲", "3" : "۳", "4" : "۴", "5" : "۵", "6" : "۶", "7" : "۷", "8" : "۸", "9" : "۹"]
var str : String = text
for (key,value) in numbersDictionary {
str = str.replacingOccurrences(of: key, with: value)
}
return str
}
此方法也可用于替换文本中的数字,无需将纯数字转换为波斯语。
/** * 在此函数中将阿拉伯数字转换为英文数字。 * @sample stringValue : 传递字符串值。 */
fun numberArabicToEnglish(stringValue: String): String {
val value = stringValue
.replace("١", "1")
.replace("٢", "2")
.replace("٣", "3")
.replace("٤", "4")
.replace("٥", "5")
.replace("٦", "6")
.replace("٧", "7")
.replace("٨", "8")
.replace("٩", "9")
.replace("٠", "0")
return value
}`enter code here`
将数字转换为任何语言环境
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "ar")
if let localized = formatter.string(from: NSNumber(value: comp.0)) {
return "\(localized)"
}
示例:
(lldb) po formatter.string(from: NSNumber(value: 0)) ▿ Optional - some : "٠"
(lldb) po formatter.string(from: NSNumber(value: 1)) ▿ Optional - some : "١"
(lldb) po formatter.string(from: NSNumber(value: 2)) ▿ Optional - some : "٢"
(lldb) po formatter.string(from: NSNumber(value: 3)) ▿ Optional - some : "٣"
(lldb) po formatter.string(from: NSNumber(value: 4)) ▿ Optional - some : "٤"
(lldb) po formatter.string(from: NSNumber(value: 5)) ▿ Optional - some : "٥"
(lldb) po formatter.string(from: NSNumber(value: 6)) ▿ Optional - some : "٦"
(lldb) po formatter.string(from: NSNumber(value: 7)) ▿ Optional - some : "٧"
(lldb) po formatter.string(from: NSNumber(value: 8)) ▿ Optional - some : "٨"
(lldb) po formatter.string(from: NSNumber(value: 9)) ▿ Optional - some : "٩"
(lldb) po formatter.string(from: NSNumber(value: 10)) ▿ Optional - some : "١٠"
Swift 5 及以上
如果你想创建 UILabel 的子类:
class LocalizedNumbersLabel: UILabel {
override var text: String? {
didSet {
if let text = text {
let numbersDictionary : Dictionary = ["0" : "۰","1" : "۱", "2" : "۲", "3" : "۳", "4" : "۴", "5" : "۵", "6" : "۶", "7" : "۷", "8" : "۸", "9" : "۹", "." : ","]
var str = text
for (key,value) in numbersDictionary {
str = str.replacingOccurrences(of: key, with: value)
}
if(str != self.text){
self.text = str
}
} else {
}
}
}
}
在swift5
func getNumerals(num: Int) -> String {
if getLanguage() == "ar" // You can set locale of your language
{
let number = NSNumber(value: num)
let format = NumberFormatter()
format.locale = Locale(identifier: "ar") // You can set locale of your language
let formatedNumber = format.string(from: number)
return formatedNumber!
} else {
return "\(num)"
}
}
从任何语言到任何语言
您可以将任何语言的数字转换成任何其他语言,例如:
"سلام 12345".convertedDigitsToLocale(Locale(identifier: "FA")) /* سلام ۱۲۳۴۵ */
"Hello ۱۲۳۴۵".convertedDigitsToLocale(Locale(identifier: "EN")) /* Hello 12345 */
分机:
具有以下扩展名:
extension String {
private static let formatter = NumberFormatter()
func clippingCharacters(in characterSet: CharacterSet) -> String {
components(separatedBy: characterSet).joined()
}
func convertedDigitsToLocale(_ locale: Locale = .current) -> String {
let digits = Set(clippingCharacters(in: CharacterSet.decimalDigits.inverted))
guard !digits.isEmpty else { return self }
Self.formatter.locale = locale
let maps: [(original: String, converted: String)] = digits.map {
let original = String([=11=])
guard let digit = Self.formatter.number(from: String([=11=])) else {
assertionFailure("Can not convert to number from: \(original)")
return (original, original)
}
guard let localized = Self.formatter.string(from: digit) else {
assertionFailure("Can not convert to string from: \(digit)")
return (original, original)
}
return (original, localized)
}
var converted = self
for map in maps { converted = converted.replacingOccurrences(of: map.original, with: map.converted) }
return converted
}
}