swift - How to convert english numbers inside a string to Persian/Arabic numbers in Objective-C?

Swift - How to convert english numbers inside a string to Persian/Arabic numbers in Objective-C?

To convert English numbers inside a string to Persian or Arabic numbers in Objective-C, you can create a simple function that replaces each English digit with its equivalent Persian or Arabic digit. Here's an example implementation:

// Function to convert English numbers to Persian/Arabic numbers NSString *convertToPersianArabicNumbers(NSString *inputString) { NSDictionary *numberMappings = @{ @"0": @"۰", @"1": @"۱", @"2": @"۲", @"3": @"۳", @"4": @"۴", @"5": @"۵", @"6": @"۶", @"7": @"۷", @"8": @"۸", @"9": @"۹" }; NSMutableString *outputString = [NSMutableString stringWithString:inputString]; // Replace each English digit with its Persian/Arabic equivalent [numberMappings enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) { [outputString replaceOccurrencesOfString:key withString:value options:NSLiteralSearch range:NSMakeRange(0, outputString.length)]; }]; return outputString; } 

You can then use this function to convert English numbers inside a string to Persian or Arabic numbers:

NSString *inputString = @"12345"; NSString *convertedString = convertToPersianArabicNumbers(inputString); NSLog(@"Converted string: %@", convertedString); // Output: Converted string: ۱۲۳۴۵ 

In this implementation:

  • We define a dictionary (numberMappings) that maps each English digit (0-9) to its corresponding Persian or Arabic digit.
  • We create a mutable copy of the input string (outputString) and iterate through the dictionary to replace each English digit with its Persian or Arabic equivalent using replaceOccurrencesOfString:withString:options:range: method.
  • Finally, we return the converted string.

You can adjust the numberMappings dictionary to use either Persian or Arabic numbers, depending on your requirements.

Examples

  1. Objective-C code to convert English numbers to Persian/Arabic numbers in a string

    Description: This Objective-C code snippet demonstrates how to convert English numbers inside a string to Persian/Arabic numbers.

    NSString *inputString = @"There are 3 apples and 5 oranges."; NSDictionary *numberMapping = @{ @"0": @"۰", @"1": @"۱", @"2": @"۲", @"3": @"۳", @"4": @"۴", @"5": @"۵", @"6": @"۶", @"7": @"۷", @"8": @"۸", @"9": @"۹" }; NSMutableString *resultString = [inputString mutableCopy]; for (NSString *key in numberMapping) { [resultString replaceOccurrencesOfString:key withString:numberMapping[key] options:NSLiteralSearch range:NSMakeRange(0, [resultString length])]; } NSLog(@"%@", resultString); 
  2. Converting English numbers to Persian/Arabic numbers in a string using Objective-C

    Description: This code snippet converts English numbers found inside a string to their equivalent Persian/Arabic numbers in Objective-C.

    NSString *inputString = @"I have 2 apples and 3 oranges."; NSString *outputString = [inputString stringByReplacingOccurrencesOfString:@"[0-9]" withString:@"\\U066\\U06F" options:NSRegularExpressionSearch range:NSMakeRange(0, [inputString length])]; NSLog(@"%@", outputString); 
  3. Objective-C method to replace English numbers with Persian/Arabic numbers in a string

    Description: This method in Objective-C replaces English numbers with their Persian/Arabic counterparts in a given string.

    - (NSString *)convertEnglishNumbersToPersian:(NSString *)inputString { NSDictionary *numberMapping = @{ @"0": @"۰", @"1": @"۱", @"2": @"۲", @"3": @"۳", @"4": @"۴", @"5": @"۵", @"6": @"۶", @"7": @"۷", @"8": @"۸", @"9": @"۹" }; NSMutableString *resultString = [inputString mutableCopy]; for (NSString *key in numberMapping) { [resultString replaceOccurrencesOfString:key withString:numberMapping[key] options:NSLiteralSearch range:NSMakeRange(0, [resultString length])]; } return [resultString copy]; } 
  4. Replacing English numbers with Persian/Arabic numbers in Objective-C using a category

    Description: This Objective-C category method replaces English numbers with their Persian/Arabic equivalents in a given string.

    @interface NSString (PersianNumbers) - (NSString *)convertEnglishNumbersToPersian; @end @implementation NSString (PersianNumbers) - (NSString *)convertEnglishNumbersToPersian { NSDictionary *numberMapping = @{ @"0": @"۰", @"1": @"۱", @"2": @"۲", @"3": @"۳", @"4": @"۴", @"5": @"۵", @"6": @"۶", @"7": @"۷", @"8": @"۸", @"9": @"۹" }; NSMutableString *resultString = [self mutableCopy]; for (NSString *key in numberMapping) { [resultString replaceOccurrencesOfString:key withString:numberMapping[key] options:NSLiteralSearch range:NSMakeRange(0, [resultString length])]; } return [resultString copy]; } @end 
  5. Objective-C code to convert English numbers to Persian/Arabic numbers using a loop

    Description: This Objective-C code snippet converts English numbers found inside a string to their Persian/Arabic counterparts using a loop.

    NSString *inputString = @"There are 4 books and 6 pens."; NSArray *englishNumbers = @[@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"]; NSArray *persianNumbers = @[@"۰", @"۱", @"۲", @"۳", @"۴", @"۵", @"۶", @"۷", @"۸", @"۹"]; NSMutableString *resultString = [inputString mutableCopy]; for (int i = 0; i < englishNumbers.count; i++) { [resultString replaceOccurrencesOfString:englishNumbers[i] withString:persianNumbers[i] options:NSLiteralSearch range:NSMakeRange(0, [resultString length])]; } NSLog(@"%@", resultString); 
  6. Converting English numbers to Persian/Arabic numbers using an NSDictionary in Objective-C

    Description: This Objective-C code snippet uses an NSDictionary to map English numbers to their Persian/Arabic equivalents in a given string.

    NSString *inputString = @"The price is $50."; NSDictionary *numberMapping = @{ @"0": @"۰", @"1": @"۱", @"2": @"۲", @"3": @"۳", @"4": @"۴", @"5": @"۵", @"6": @"۶", @"7": @"۷", @"8": @"۸", @"9": @"۹" }; NSMutableString *resultString = [inputString mutableCopy]; [numberMapping enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) { [resultString replaceOccurrencesOfString:key withString:value options:NSLiteralSearch range:NSMakeRange(0, [resultString length])]; }]; NSLog(@"%@", resultString); 

More Tags

ssrs-2008 android-shapedrawable angularjs-ng-checked nullreferenceexception css-position loops truetype gulp udev emulation

More Programming Questions

More Physical chemistry Calculators

More Dog Calculators

More Fitness Calculators

More Geometry Calculators