stringByReplacingOccurrencesOfString not working perfectly for Arabic numbers - ios

Its not replacing comma, in case of Arabic language.
NSString *strAfterReplacingComma = #"٢٠٠٫٠٠";//100.00
NSString *strAfterReplacing = [strAfterReplacingComma stringByReplacingOccurrencesOfString:#"," withString:#"."];
NSLog(#"%#", strAfterReplacing);
Can anyone please help me in solving this?

The comma in your Arabic text (٫) has a unicode number U+002C. It is the arabic decimal separator. Not a comma. The english Comma(,) has a unicode number U+066B. Hence the replace fails.
Use this ٫ instead of , in you stringByReplacingOccurrencesOfString.

Related

how to replace unicode "&aring" with norwegian character å

I want to replace the unicode: &aring with Norwegian character å but the following code is not helping:
[unq1 stringByReplacingOccurrencesOfString:#"å" withString:#"å" ];
It is working perfectly on my side, may be the problem is you have not save the result of this stringByReplacingOccurrencesOfString method. So try like this
unq1 = [unq1 stringByReplacingOccurrencesOfString:#"å" withString:#"å"];

Convert Escaped unicode to unicode in Objective-c

SO,
A seemingly simple question has me stumped. I have two statements:
NSLog(#"%#", #"\U0001f1ee\U0001f1f9");
NSLog(#"%#", #"\\U0001f1ee\\U0001f1f9");
The first outputs the correct emoji (Flag). The second outputs an escaped string. What conversion do I need to do to the second string to make it output the flag as well?
In other words: I have strings of escaped Unicode that I want to print out as the proper Emoji. How would I go about doing that?
I tried converting to NSUTF8StringEncoding NSData and then back to NSString, I tried using NSNonLossyASCIIStringEncoding, no joy. I must be using them wrong...
Thanks for any help!
Easy. Use -stringByRemovingPercentEncoding.
NSString * string = #"\\U0001f1ee\\U0001f1f9" ;
NSLog( #"%#", [string stringByRemovingPercentEncoding]);

NSString stringWithFormat not working with special chars like currency symbols

I have been having a lot of trouble with NSString's stringWithFormat: method as of late. I have written an object that allows you to align N lines of text (separated by new lines) either centered, right, or left. At the core of my logic is NSString's stringWithFormat. I use this function to pad my strings with spaces on the left or right of individual lines to produce the alignment I want. Here is an example:
NSString *str = #"$3.00" --> 3 dollars
[NSString stringWithFormat:#"%8s", [str cStringUsingEncoding:NSUnicodeStringEncoding]] --> returns --> " $3.00"
As you can see the above example works great, I padded 3 spaces on the left and the resulting text is right aligned/justified. Problems begin to arise when I start to pass in foreign currency symbols, the formatting just straight up does not work. It either adds extra symbols or just returns garbage.
NSString *str = #"Kč1.00" --> 3 Czech Koruna (Czech republic's currency)
[NSString stringWithFormat:#"%8s", [str cStringUsingEncoding:NSUnicodeStringEncoding]] --> returns --> " Kč1.00"
The above is just flat out wrong... Now I am not a string encoding expert but I do know NSString uses the international standardized unicode encoding for special characters well outside basic ASCII domain.
How can I fix my problem? What encoding should I use? I have tried so many different encoding enums I have lost count, everything from NSMACOSRomanEncoding to NSUTF32UnicodeBigEndian.. My last resort will be to just completely ditch using stringWithFormat all together, maybe it was only meant for simple UTF8Strings and basic symbols.
If you want to represent currency, is a lot better if you use a NSNumberFormatter with currency style (NSNumberFormatterCurrencyStyle). It reads the currentLocale and shows the currency based on it. You just need to ask its string representation and append to a string.
It will be a lot easier than managing unicode formats, check a tutorial here
This will give you the required result
NSString *str = #"Kč1.00";
str=[NSString stringWithFormat:#"%#%8#",[#" " stringByPaddingToLength:3 withString:#" " startingAtIndex:0],str];
Out Put : #" Kč1.00";
Just one more trick to achieve this -
If you like use it :)
[NSString stringWithFormat:#"%8s%#",[#"" cStringUsingEncoding:NSUTF8StringEncoding],str];
This will work too.

How to disallow special characters but allow Asian/Middle Eastern characters in a UITextField

I am using a UITextField for users to enter usernames, and would like to restrict special characters except for periods and underscores. I was initially set on using the solution from this SO question, until I realized that I do not want to restrict to only alpha-numeric characters, but also allow Asian and Middle Eastern languages characters as well. Is there a way that I would be able to accomplish this?
Thanks!
Update:
Per rmaddy's suggestion, here is what I am presently using:
- (BOOL)userNameIsAcceptable: (NSString *)userNameInputted
{
NSCharacterSet *userNameAcceptedInput = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSString *filteredUserName = [[userNameInputted componentsSeparatedByCharactersInSet:userNameAcceptedInput] componentsJoinedByString:#""];
NSLog(#"The filtered result %#", filteredUserName);
return [userNameAcceptedInput isEqual:filteredUserName];
}
Use the solution from the other question but instead of building the character set from the fixed letters, use the standard NSCharacterSet alphanumericCharacterSet.
Maybe testing unicode chars from strings manually in loop against ranges containing unicode sets you need?
I did quick check, and it seems that unicode sets of Japanese letters are usually densely packed, except some special characters - I've looked here http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml but I guess similar should be valid to other languages as well.

Writing a unicode character with NSString

I'm using the symbol font Symbolicons instead of images in a new project. However, it seems that any code over 4 characters can't be set using NSString.
Example:
self.saveDealButton.titleLabel.font = [UIFont fontWithName:#"SS Symbolicons" size:31.0f];
[self.saveDealButton setTitle:#"\u1F4E5" forState:UIControlStateNormal];
Will not work, however:
self.shareButton.titleLabel.font = [UIFont fontWithName:#"SS Symbolicons" size:31.0f];
[self.shareButton setTitle:#"\uF601" forState:UIControlStateNormal];
Works fine. How can I get NSString to recognize the extra bit?
For those characters in the Supplementary Multilingual Plane, as in your example, use the uppercase U in the escape string and followed by eight hex code. So it should be written as \U0001F4E5.
In iOS unicode characters belong to a 16bit representation \u , with n between 0000 and ffff in hexadecimal notation.
In your example \uF601 represents one character and you could add another character by adding another sequence \uF601\uF602 etc.
For me it seems that you misunderstood the escape syntax?

Resources