I've written and used this code in my apps whenever needed to convert any Western Arabic numerals into Eastern Arabic (or better say, Persian/Farsi) numerals in any Persian text.
It works fine as expected, but I'm having a look at it now to reuse it, and I'm wondering if I could optimize this to reduced the repeated lines of code, and if so, how?
The header file, FarsiNumerals.h:
@interface FarsiNumerals : NSObject
+(NSString *)convertNumeralsToFarsi:(NSString *)englishNumeralString;
@end
and the implementation file, FarsiNumerals.m:
@implementation FarsiNumerals
+(NSString *)convertNumeralsToFarsi:(NSString *)englishNumeralString
{
NSString *farsiNumeral;
farsiNumeral = [englishNumeralString stringByReplacingOccurrencesOfString:@"1" withString:@"۱"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"2" withString:@"۲"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"3" withString:@"۳"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"4" withString:@"۴"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"5" withString:@"۵"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"6" withString:@"۶"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"7" withString:@"۷"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"8" withString:@"۸"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"9" withString:@"۹"];
farsiNumeral = [farsiNumeral stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
return farsiNumeral;
}
@end
It's a simple piece of code as you see; I'd call the method wherever needed and it'd replace the numerals with the correct ones for me for optimized text display purposes, and I'd like to know if I can somehow make this even shorter?
englishNumeralString
a string with a (single) number, or is it arbitrary text with some numbers embedded? It is not clear (to me) from your description “convert any Western Arabic numerals into Eastern Arabic ... numerals in any Persian text.” \$\endgroup\$