Eg :"Transport & Logistics" to "Transport \u0026 Logistics"
The thing you are trying to do is to convert Text to Unicode Hex Representation.
the code below:
NSString *amp = #"&";
char ch = [amp characterAtIndex:0];
NSLog(#"amp is %04x", ch);
printing out in console: amp is 0026
(4 in %04x is number of characters to log)
now the deal is to represent 0026 as \u0026,
you can use stringWithFormat method of NSString, to achieve the result, if you want it as string.
And it is not clear, if you need to convert only ampersand - you should search for it in string.
The & is \u0026 in unicode characters and the bootstrap will accept %26 as &
Related
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]);
We have key-value pair in Localization.string file.
"spanish-key" = "Espa\u00f1ol";
When we fetch and assign to label then app displays it as "Espau00f1ol".
Doesn't work.
self.label1.text= NSLocalizedString(#"spanish-key", nil);
It works- shows in required format.
self.label1.text= #"Espa\u00f1ol";
What could be the problem here when we use
NSLocalizedString(#"spanish-key", nil)?
If we set \U instead of \u, then it works.
"spanish-key" = "Espa\U00f1ol";
When to use "\Uxxxx" and "\uxxxx"?
NSString literals and strings-files use different escaping rules.
NSString literals use the same escape sequences as "normal" C-strings, in particular
the "universal character names" defined in the C99 standard:
\unnnn - the character whose four-digit short identifier is nnnn
\Unnnnnnnn - the character whose eight-digit short identifier is nnnnnnnn
Example:
NSString *string = #"Espa\u00F1ol - \U0001F600"; // Español - 😀
Strings-files, on the other hand, use \Unnnn to denote a UTF-16 character,
and "UTF-16 surrogate pairs" for characters > U+FFFF:
"spanish-key" = "Espa\U00f1ol - \Ud83d\Ude00";
(This is the escaping used in "old style property lists", which you can see when printing
the description of an `NSDictionary.)
This (hopefully) answers your question
When to use "\Uxxxx" and "\uxxxx"?
But: As also noted by #gnasher729 in his answer, there is no need to use Unicode
escape sequences at all. You can simply insert the Unicode characters itself,
both in NSString literals and in strings-files:
NSString *string = #"Español - 😀";
"spanish-key" = "Español - 😀";
Just write the string in proper Unicode in Localization.string.
"spanish-key" = "Español";
I have some content like 2ofsjw0234lnc.jpg\t2m03fcsmaokwf.jpg\n want to encode as a url Parameter,so I use code like bellowed.
NSString * attachsString = [_attachments stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
the output is 2ofsjw0234lnc.jpg%5ct2m03fcsmaokwf.jpg%5cn
it is not my wanted, 2ofsjw0234lnc.jpg%092m03fcsmaokwf.jpg%0d%0a, the Escape character are not convert as i wanted. only "\" have been converted.
so what could you give some advice?
If you put \t in a string literal, then at compile time, this gets converted to a tab. Then stringByAddingPercentEscapesUsingEncoding: would convert the tab to %09. However, if at runtime, the string has the two characters \t, then this is not treated any different than any other two characters. In this case, stringByAddingPercentEscapesUsingEncoding: simply sees the backslash and converts it to %5c.
If you want to convert \t or \n strings (not characters) at runtime, then you should use:
// note the double backslash for the tab
string = [string stringByReplacingOccurrencesOfString:#"\\t" withString:#"%09"];
Repeat for the \n.
I want to escape Unicode word to be used in URL to make HTTPRequest, for example want to convert "محمود" to "%D9%85%D8%AD%D9%85%D9%88%D8%AF" I noticed that each character has converted to two HEX
Thanks a lot
Convert to UTF-8, then url-encode chars not in [[:alnum:]].
\Url-encoding is where a character is converted into %<HIGHNIBBLE><LOWNIBBLE> form, where HIGHNIBBLE = (ch >> 4) & 0x0F and LOWNIBBLE = (ch & 0x0F).
Look into RFC 1738 (S) 2.2 for more details.
Because it looks like you're using java, you'll have to work with byte[] instead of String or char[].
Given:
CR = %0d = \r
LF = %0a = \n
What does
%3E,
%3C
Mean?
They are URL encoded characters. %3C is <, %3E is >
More info on URL Encoding, and a chart of some of the lower ASCII values.
paste
javascript:alert(unescape("%3E"))
into a browser's address bar and hit Return to find out ;)
The two digits after the % is an ASCII code represented in hexadecimal.