I am trying to compare two hindi string but it says string is not equal below is the code:
if([#"हड़ताल" isEqualToString:#"हड़ताल"]){
NSLog(#"String is equal");
}else{
NSLog(#"String is not equal");
}
How can I compare two non english strings ?
Thanks in advance
The strings LOOK the same but I believe the unicode characters are slightly different so it returns NO currently as you stated...
I tested it, it returned NO
I copied and pasted the first string over the second string and it returned YES
So in conclusion.. it currently isn't the same string due to encoding.
Related
I have a string with random names with an invisible decimal value as prefix . The decimal = the names length. I need to retrieve the names. Obviously they are of different length. I want the names in an array so my idea is to use stringByReplacingOccurrencesOfString:withString. I implement the word "trunk" at the beginning and end of names. Though I am having trouble accessing the index corresponding at the end of the name (decimal value), here is my code :
trimmed1 = [[trimmed1 stringByReplacingOccurrencesOfString:sp withString:#"trunk"]mutableCopy];
NSString *trunk = #"trunk%d";// add the ghost decimal at the end of prefix in order to get its value;
NSRange range =[trimmed1 rangeOfString:trunk];
int ghost= [trunk characterAtIndex:5];
NSMutableString *mu = [NSMutableString stringWithString:trimmed1];
[mu insertString : #"trunk" atIndex :range.location+range.length+ ghost];
I get the error [__NSCFString insertString:atIndex:]: Range or index out of bounds.
You are misunderstanding what %d means.
In a format used for creating a string it means "insert the value of an integer argument formatted as a string".
When matching one string against another it means "match the characters %d", I.e. it is not special in anyway.
You are getting an error as your string does not contain the characters "trunk%d". If you check the return value of rangeOfString: you will find it is returning a failure indication - read the documentation for how to test for that value.
For the simple task of matching an arbitrary decimal number trying looking at the NSString method rangeOfCharactersFromSet:.
You can also solve this problem with the classes NSScanner and NSRegularExpression.
HTH
I have an NSString that contains a value "\U2212" instead of "-" which is coming from API. When I tried to replace this string with needed character using subString occurrence method it shown error. So how do I replace my NSString that contains "\U2212" with "-". I tried the following code. Please help me. I searched many things but nothing helped. Thanks in advance.
input:"(UTC\U221206:00) Canada/Central"
Desired output:"(UTC-06:00) Canada/Central"
code:
NSString *timezoneDisplayValue = [timezone valueForKey:#"tomeZoneDisplayValue"];
timezoneDisplayValue = [timezoneDisplayValue stringByReplacingOccurrencesOfString:#"\U2212" withString:#"-"];
below code returns YES or NO, whether your string has any encoding...
[apiString canBeConvertedToEncoding:NSASCIIStringEncoding];
If NO is returned, then convert with the correct encoding type :
[apiString dataUsingEncoding:NSUTF16BigEndianStringEncoding];
I have searched many hours how to compare this two strings ( and have str1 equal str2 == YES):
This string is given by a user input in my interface (search bar)
NSString *str1 = #"11,2";
This string is a property of my model object
NSString *str2 = #"11.2";
i have tested this but it's not working
if ([str1 rangeOfString:str2 options:options range:NSMakeRange(0, [str1 length]) locale:[NSLocale currentLocale]].location == NSNotFound
Witch is the best solution to compare the two NSStrings and have str1 equal str2 that returns YES ?
Edit :
I know that i can do it simply by replacing the "," by "." and the do the comparaison, but i am searching an elegant solution that take the fact that 11.2 and 11,2 are the same in different locales ( using NSDecimalNumber for example).
You've just essentially answered your own question - use NSDecimalNumber to parse the user's input according to the current locale. Parse your string using your locale (or better yet keep it as a floating point value. That should solve your problem apart from the standard issue that comparing floating point values for exact equality is not advised, though NSDecimalNumber may help you address that. HTH
If you can trust user's locale settings on the machine you could use a NSScanner instance to scan the number string and use a float or double as your model so that you can perform proper searching.
When I insert Hebrew (LTR) string into NSMutableArrey, the string is distorted somehow.
What do I do?
NSString *peace = #"שלום";
NSLog(#"peace - %#", peace);
NSMutableArray *peaceArrey = [[NSMutableArray alloc]initWithCapacity:1];
[peaceArrey addObject:peace];
NSLog(#"peaceArrey - %#",peaceArrey);
And here is the log:
peace - שלום
peaceArrey - (
"\U05e9\U05dc\U05d5\U05dd"
)
everything should be ok, try NSLog(#"%#", peaceArrey[0]);
the result you are seeing is just the way NSArrays are printed: unicode chars are represented as their codes.
Don't mistake the representation that gets logged as the actual value of the object. NSArray's description is in an old-style property list format. Among other things, that means that non-ASCII values in strings are represented as escape sequences. You're seeing the Unicode characters as a series of UTF-16 code units expressed as escape sequences.
When using the %# format specifier NSLog calls description on the argument to log the string.
In case of a plain string this method just returns the string:
NSLog(#"string: %#", #"שלום");
// prints שלום
If you, on the other hand, put the string into an array, the NSArray's description method is called which, in turn, calls descriptionWithLocale:indent:.
This method just creates a property list formatted string. It uses the NSPropertyListOpenStepFormat which is ASCII encoded. That's why it has to escape the hebrew unicode characters.
I need to convert values like 1393443048683555715 to HEX. But, first of all, i cann't display it as decimal using NSLog(), for example.
Ok, it works:
NSLog(#"%qu", 1393443048683555706);
But what about converting to HEX. What type i have to use to store this big value?
NSLog([NSString stringWithFormat: #"%x", 1393443048683555706]);
// result eb854b7a. It's incorrect result!
but i forgot to say that this big number represented as string #"1393443048683555706" (not int)
You can use %qi and %qu format specifiers with NSLog to display 64-bit integers. Your constant appears to fit in 64-bit signed number, with the limits of:
[−9223372036854775808 to 9223372036854775807]
The "x" format specifier is for 32-bit numbers; you need to use either "qx" or "qX" (depending on whether you want the letter values to be uppercase or not). These are the formatters for unsigned long long values, see:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1
Next, you should not pass a string as you have done above directly to NSLog - this can cause a crash.
NSLog(string); // bad!!
NSLog(#"%#", string); // good
So if your value comes as a string, you'll want to do this:
NSString *longNumber = #"1393443048683555706";
NSLog(#"%qx", [longNumber longLongValue]);
If the string value can't be coerced to a number, longLongValue will return 0. I'll leave it to you do handle the error (and bounds) checking - see NSString for details.
If you want to save the hex value as a string, do this:
NSString *hexRepresentation = [NSString stringWithFormat:#"%qx", [longNumber longLongValue]];
Again, best to take care for error handling.