I need to see some string with Unicode characters in the Xcode console when I do NSLog(#"some unicode characters.."). Eg: abc\u0001xyz\u0002pqr… But Xcode console only shows the abcxyzpqr. It doesn't show the intermediate Unicode characters. Does anyone know how to view this?
I know, it's a long time since you asked but try this if you're still looking for an answer :
NSString *demo=[[NSString alloc] initWithData:
[NSData dataWithBytes:myUnicodeString length:itsLength]
encoding:NSUnicodeStringEncoding];
NSLog(#"%#",demo);
Hope this help.
NSString* strOld=[NSString stringWithFormat:#"%#",responseObject];
NSLog(#"%#",[NSString
stringWithCString:[strOld cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding]);
NSString *string = #"Hi \u00B7 there!";
lblString.text=string;
Related
I want to have a percentage sign in my string after a digit. Something like this: 75%.
How can I have this done? I tried:
[NSString stringWithFormat:#"%d\%", someDigit];
But it didn't work for me.
The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.
The escape code for a percent sign is "%%", so your code would look like this
[NSString stringWithFormat:#"%d%%", someDigit];
Also, all the other format specifiers can be found at Conceptual Strings Articles
If that helps in some cases, it is possible to use the unicode character:
NSLog(#"Test percentage \uFF05");
The accepted answer doesn't work for UILocalNotification. For some reason, %%%% (4 percent signs) or the unicode character '\uFF05' only work for this.
So to recap, when formatting your string you may use %%. However, if your string is part of a UILocalNotification, use %%%% or \uFF05.
seems if %% followed with a %#, the NSString will go to some strange codes
try this and this worked for me
NSString *str = [NSString stringWithFormat:#"%#%#%#", #"%%",
[textfield text], #"%%"];
uese following code.
NSString *searchText = #"Bhupi"
NSString *formatedSearchText = [NSString stringWithFormat:#"%%%#%%",searchText];
will output: %Bhupi%
iOS 9.2.1, Xcode 7.2.1, ARC enabled
You can always append the '%' by itself without any other format specifiers in the string you are appending, like so...
int test = 10;
NSString *stringTest = [NSString stringWithFormat:#"%d", test];
stringTest = [stringTest stringByAppendingString:#"%"];
NSLog(#"%#", stringTest);
For iOS7.0+
To expand the answer to other characters that might cause you conflict you may choose to use:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
Written out step by step it looks like this:
int test = 10;
NSString *stringTest = [NSString stringWithFormat:#"%d", test];
stringTest = [[stringTest stringByAppendingString:#"%"]
stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]];
stringTest = [stringTest stringByRemovingPercentEncoding];
NSLog(#"percent value of test: %#", stringTest);
Or short hand:
NSLog(#"percent value of test: %#", [[[[NSString stringWithFormat:#"%d", test]
stringByAppendingString:#"%"] stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);
Thanks to all the original contributors. Hope this helps. Cheers!
I am writing an app with both english and french support. The app requests information from a server and the server response uses JSON. I am using the JSONKit library to parse the response but the strings its parsing from the french responses look like this:
Membres –Économisez 5% sur les services et jusqu’à 15% sur d’autres produits
How do I decode the special characters? so that I get this:
Membres –Économisez 5% sur les services et jusqu’à 15% sur d’autres produits
I looked at the API for NSString and tried some of the instance methods but I don't know much about character encodings and I ended up getting some weird results. So if you can also provide a brief explanation on character encodings I'd really appreciate it.
Thanks in advance
Check out these NSString categories
This solution worked well for me if you're still using Objective C code.
- (NSString *)decodeHtmlStringValue
{
NSData *encodedString = [self dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *htmlString = [[NSAttributedString alloc] initWithData:encodedString
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];
return [htmlString string];
}
If your using swift
https://stackoverflow.com/questions/25607247/how-do-i-decode-html-entities-in-swift
NSString stringByReplacingPercentEscapesUsingEncoding with the correct string encoding should do the magic.
[yourString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
might be a good candidate.
Hello everyone
I have an objective-c dilema :P
I am quite new to objective-c, and I have tried searching for an answer, but to no avail.
So, here is my situation.
I will put the code here now, or else it won't make as much sense.
I am putting down what I need, this code does not work right now, and I will get to why later
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM"];
NSString *monthString = [formatter stringFromDate:date];
NSLog(#"MONTH STRING %#", monthString);
NSString *baseURLStr = #"http://www.mysite.ca/example";
NSURL *url = [NSURL URLWithString:[baseURLStr stringByAppendingFormat:#"announcements%20%#%20%d%20carson.ashx", monthString]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[NSCalendar release];
So, this is where the problem lies... Encoded (HTML): stringByAppendingFormat:#"announcements%#%20%d%20example.ashx", monthString, day]];
Decoded it looks like this
stringByAppendingFormat:#"announcements %# %d example.ashx"]];
That should be easier to understand..
When I run with the %20's, it says "The requested document was not found"
When I run with spaces (" ") it is just white, and nothing loads.
I know the URL is correct, and if I take the variables out, it is the exact same problem, but when I move the %20's back to the baseURLStr, it works and loads just fine, so it is something to do with the HTML Codes and the "stringbyAppendingFormat" string.
Any help is appreciated!
Thanks for your time
-Kyle
whenever an NSURL returns nil (0x0) after init it is almost always related to in improper url string. And its very picky about getting a properly formatted string.
my recommendation is to simply build your string, without any escapes or url encoding, then simply call
myUrlString = [myUrlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
here is the header declaration for it
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc NS_AVAILABLE(10_3, 2_0);
this way, I always know I get it formatted the way that the NSURL class wants it.
here is an example
NSString *sUrl = #"http://www.myside.ca/example/announcements carson.ashx"; //notice the embedded space
sUrl = [sUrl stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:sUrl];
If I understand you correctly, you're trying to generate a string with literal "%20"s embedded in it. The "%" character is special in format strings. If you want to insert a literal percent character, you need to escape it by putting two consecutive "%"s. Like
[NSURL URLWithString:[baseURLStr stringByAppendingFormat:#"foo%%20bar"]];
That would append "foo%20bar" to the end of the string.
You probably just need to encode the percent signs. Try: stringByAppendingFormat:#"announcements%%20%#%%20%d%%20carson.ashx"]
If you're not using printf-style formats, don't use stringByAppendingFormat:. Use stringByAppendingString: instead.
Second, is the resulting URL really supposed to be http://www.myside.ca/exampleannouncements%20%#%20%d%20carson.ashx? Or is there supposed to be a slash in the middle: http://www.myside.ca/example/announcements%20%#%20%d%20carson.ashx?
Also, http://www.myside.ca/exampleannouncements%20%#%20%d%20carson.ashx is an invalid URL. The percent signs that are not part of an escape (e.g. not part of %20) must themselves be encoded, as %25. Technically, the # should also be escaped (as %40), but IIRC NSURL will let that slide.
What is the best method to convert from a C++ basic_string<wchar_t> object with UTF16 encoding to an Objective-C NSString object?
Can I cast from wchar_t* to char* like so and still have stringWithCString use the string correctly?
[NSString stringWithCString:(char*)wideCharBasicString.c_str() encoding:NSUTF16StringEncoding];
Thank you,
Shane
If the buffer is UTF-16 encoded, you could do this:
NSData* data = [[[NSData alloc] initWithBytesNoCopy:buffer
length:length
freeWhenDone:NO] autorelease];
NSString* result = [[NSString alloc] initWithData:data
encoding:NSUTF16LittleEndianStringEncoding];
stringWithCString looks like it barfs on multi-byte character buffers, stopping at the first NULL byte it finds.
[UPDATE]
I filed a bug with apple, and this is the expected behaviour, apparently. stringWithCString only supports 8-bit encodings and will stop at the first zero byte.
I used the following to do the converison:
NSString *myNSString = [NSString stringWithFormat:#"%S",wideCharBasicString.c_str()];
Also if anyone needs to use this like I did, the flag -fshort-wchar turns wchar_t to 16-bit on iOS.
This is the least ugly way to do it (utf16):
NSString* nsstr = [NSString stringWithCharacters:str.c_str() length:str.length()];
I want to decode my string. I have used parsing and get a string from RSS feed. In a string these special characters are not allowed &,<,> in my app. In server side encoding those characters and give it to the string. So now i got the string like,
Actual String : <Tom&Jerry> (only these characters are not allowed in node data & < >).
After Encoding: %3CTom%26Jerry%3E.
But i need to display the string is
<Tom&Jerry>
So how can i decode the string.
Please help me out.
Thanks.
Use the -stringByReplacingPercentEscapesUsingEncoding: method.
[#"%3CTom%26Jerry%3E"
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Look for
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Or by example:
NSString *input = #"Hello%20World";
NSString *output = [text stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"%# becomes %#",input,output);
Log: Hello%20World becomes Hello World
I got the answer and my code is,
NSString *currentString =#"%3CTom%26Jerry%3E";
NSString * decodeString = [currentString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
lblTitle.text = decodeString;
Thanks.