I am receiving a nsstring that is not properly encoded like "mystring%201, where must be "mystring 1". How could I replace all characters that could be interpreted as UTF8? I read a lot of posts but not a full solution. Please note that nsstring is already encoded wrong and I am not asking about how to encode char sequence. Thank you.
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding is what you want. basically use it like so:
newString = [myString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Check the Strings and Non-ASCII Characters section of Formatting String Objects:
NSString *s = [NSString stringWithUTF8String:"Long \xe2\x80\x94 dash"];
lblDate.text=[NSString stringWithCString:[[arrTripDetail valueForKey:Param_vCurrencySymbol] UTF8String] encoding:NSUTF8StringEncoding];
U can Convert & get Custom Emoji to string
eg :
input : \U20b9
Output: ₹
Do you want just percent encoding/decoding or full URL encoding/decoding? -(NSString*)stringByReplacingPercentEscapesUsingEncoding: will work if it is just percent encoding, but if there is full URL encoding there (so, for example a space could be either %20 or +) then you'll need something like the url decoding in the three20 library (or search on there, there are lots of examples of how to do it such as URL decoding/encoding NSString).
Related
I am getting some weird characters when converting a NSArray containing NSDictionaries to a JSON string.
I tried using both SBJson and NSJSONSerialization with the same result.
The NSDictionary is populated with the content of the address book, with the contact name, email and phone number, and are mostly in hebrew.
The characters look like this:
\327\237
I could not find any information about this, help anyone?
Thanks in advance!
EDIT *
Here is a snippet of the JSON:
[
{"fname":"סתם טקסט"},
{"fname":"סתם טקסט"},
{"fname":"נ\327\231ר"}
]
its supposed to be:
[
{"fname":"סתם טקסט"},
{"fname":"סתם טקסט"},
{"fname":"ניר"}
]
And i am getting the JSON by using the following code:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:ContactsArray options:NSJSONReadingMutableLeaves error:&err];
NSLog(#"JSON: %#", [NSString stringWithUTF8String:[jsonData bytes]]);
These characters are octal escape codes. I prefer to look at things in hex. \327 and \237 are 0xD7 and 0x9F in hex.
I looked up U+00D7 and U+009F (unicode characters). They are MULTIPLICATION SIGN and APPLICATION PROGRAM COMMAND. That doesn't make sense in this context, so a straight conversion is not the way to go.
Next, I thought UTF-8 encoding. D7 9F decodes as U+05DF. This is HEBREW LETTER FINAL NUN. That makes sense in this context.
So, I'm guess the data you are seeing in UTF-8 characters that are not understood and octal escaped. JSON doesn't support octal escapes, so I'm guessing it's NSLog() or whatever you are using to print the JSON that is doing the escaping.
Our code calls stringWithUTF8String but some data we have uses an octal sequence \340 in the string. This causes some code to break because we never expect the function to return nil. I did some research and found that any octal sequence from \200-\777 will give the same result. I know I can handle this returning nil but I want to understand why it would return nil, and what those octal escapes are interpreted as.
NSString *result = [NSString stringWithUTF8String:"Mfile \340 xyz.jpg"];
running this code return nil for result. It appears that to code defensively we will have to check null results for this everywhere where we use it which seems unfortunate. The documentation for the function does not say anything about returning nil as a possibility. I would bet that there is a lot of code out there that does not check for it either.
The UTF-8 Character Table doesn't have an entry for \340. You need to use the ASCII encoding for this. Do,
NSString * result = [NSString stringWithCString:"Mfile \340 xyz.jpg" encoding:NSASCIIStringEncoding];
NSLog(#"%#", result);
If you want iOS to handle it as UTF-8 you have to make sure it's valid UTF-8 characters you pass to it, so you may need to convert the octal characters to something human readable first.
I added a category which is called safeStringWithUTF8String: this is called everywhere instead it simply checks the return value for nil and returns the empty string if not valid. Not great but not sure what else to do we have to be able to handle any data passed in.
I have the following NSString:
NSString* searchURL = [NSString stringWithFormat:#"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22%#%22)%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=",symbol];
NSLog(#"URL IS: %#", searchURL);
Looks like the %22 is not being included when it is being printed:
URL IS: http://query.yahooapis.com/v1/public/yql?q=select220from2ahoo.finance.quotes2here `º≠ymbol 813020n22#20X1.000982B6P-1042009&format=json&env=http0X1.8CFB8P-1023-1.9907460.000000datatables.org-1.990746alltables.env&callback=
How can I make sure the %22 is included in my string?
If you want to include a "%" sign in a format string use "%%"
Just like in printf et al.
Read the full documentation of stringWithFormat to avoid other bad surprises ...
% is a special character in format strings. Use %% to escape literal percent signs.
% character are used in encoding space in the url formation.If you want to request any url you need to encoding that with stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding
. So the % sign that are appearing are the encoded form of space.So implement accordingly by clearing this fundamental
I have been having some problem with the stringByAddingPercentEscapesUsingEncoding: method.
Here's what happens:
When I try to use the method to convert the NSString:
"..City=Cl&PostalCode=Rh6 0Nt"
I get this this..
"City=Cl&PostalCode=Rh62t"
It should be:
"..City=Cl&PostalCode=Rh6%200Nt"
What can I do about this? Thanks in advance !!
For me, this:
NSString *s=[#"..City=Cl&PostalCode=Rh6 0Nt" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"s=%#",s);
... outputs:
s=..City=Cl&PostalCode=Rh6%200Nt
You're most likely using the wrong encoding.
This happens when you're trying to encode to NSASCIIStringEncoding a string with characters not supported by ASCII.
Make sure you're encoding to NSUTF8StringEncoding, if the string can contain UTF8 characters or the method would return nil.
I have a string here like so:
textbox.text =#"Your name is"
then I want to add right after "your name is" a variable that displays text.
so in Visual Basic I learned it like this:
textbox.text =#"Your name is" & variable1.
But now I can see that it doesn't work like that in Cocoa.
textbox.text = [NSString stringWithFormat:#"Your name is %#", variable1];
Read the documentation for stringWithFormat: to learn about string format specifiers. Basically, you have a format string that contains codes like %#, and the following arguments are put in place of those escape codes.
It has the same syntax as the old C-style printf() function. Cocoa's logging function, NSLog(), also works the same way.
If you need to combine a lot of strings together, try also reading about NSMutableString.
You could also do:
textbox.text = [#"Your name is " stringByAppendingString:variable1];
But if you have to concatenate more than two things, stringWithFormat: is much more concise.
Use NSString's stringByAppendingString: method:
textbox.text = [#"Your name is" stringByAppendingString:variable1];