Now I have a string like this "\u7c6e\u54c1\u23a8\u8357/\u4e01\u8f77\u7ba2\u7409" in Xcode, then I want convert it to "籮品⎨荗/丁轷箢琉" by Objective-C.
But I can't find any API to finish.
What should I do? To get a string content instead of string literal.
My application does not need to convert, my situation is that: I have a response from server
name = "\U672a\U6765\U822a\U8def";
I just print log:
NSString *n = dict[#"name"];
NSLog(#"%#",n); //未来航路
I think your application is the same.
The right answer is:
NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:#"\\u" withString:#"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
NSString *tempStr3 = [[#"\"" stringByAppendingString:tempStr2] stringByAppendingString:#"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
Related
I have a string which already contains a formatter %#.
NSString *str = #"This is an %#";
I need to parse that string and to replace %# with 'example'. If I use
[NSString stringWithFormat:#"%#", str];
I get the following output:
This is an %#
I want output like:
This is an example
NSString *str = #"This is an %#";
str = [str stringByReplacingOccurrencesOfString:#"%#" withString:#"example"];
I would recommand to use the formatted string as "format"
NSString *str = #"This is an %#";
str = [NSString stringWithFormat:str, #"example"];
is working with every type. A better solution than replacing, because you can use unspecified replacings
is very usefull if you use localized.strings with x values you want to add ;)
I have a label where I have to put a string in Chinese extracted from a database, but nothing comes out. I noticed that the string is not pulled from database, while all other work correctly. What can I do?
char *subTitle= (char*)sqlite3_column_text(statement,13);
NSLog(#" The sutitle is %s", subTitle);
//The sutitle is
rowTable.sottotitolo = [[NSString alloc]initWithUTF8String: subTitle];
NSLog(#"The subtitle is %#", rowTable.sottotitolo);
//The subtitle is
Using methods other than Western alphabet?
NSLog(#"The string in chinese is %#", self.chinaTable.subtitle);
//The string in chinese is
//is not printed to the screen,but the database is written correctly
self.labelTitle.text = self.chinaTable.subtitle;
//empty out
Thanks in advance
While you retrieving your data from sqlite, instead of specifying the encoding schema, use this:
NSString *myChineseText = [NSString stringWithFormat:#"%s",(const char*)sqlite3_column_text(statement, index)];
NSLog(#"%#",myChineseText);
Hope, it'll solved your problem. :)
Try CFStringConvertEncodingToNSStringEncoding and kCFStringEncodingBig5_E.
Also see apple doc and for international
or for creating own encoding see
and this
unichar ellipsis = 0x2026;
NSString *theString = [NSString stringWithFormat:#"To be continued%C", ellipsis];
// custom encoding
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSChineseTrad);
NSData *asciiData = [theString dataUsingEncoding:encoding
allowLossyConversion:YES];
NSString *asciiString = [[NSString alloc] initWithData:asciiData
encoding:encoding];
I am trying to convert two values to string and then make a new string containing the ones i made earlier so my service can accept them.
NSString *string1 = [ NSString stringWithFormat:#"%f", locationController.dblLatitude];
NSString *string2 = [ NSString stringWithFormat:#"%f", locationController.dblLongitude];
body = [[NSString stringWithFormat:#"geoX#%##geoY#%#", string1, string2] dataUsingEncoding:NSUTF8StringEncoding];
This is the code i am using atm. The problem is that both string1 and string2 appear to be ok but the string named body appears to not working.. :< Any help ?
body is not an NSString instance here, but NSData (because you're using `dataUsingEncoding:".
If you want to see concatenation of stings in system log you should write something like that:
NSString* bodyString = [NSString stringWithFormat:#"geoX#%##geoY#%#", string1, string2];
NSData* bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
and then you can NSLog(#"Body: %#", bodyString); to see it's contents and then use bodyData for making http request.
body is not an NSString; it is an NSData because of your call to dataUsingEncoding.
I believe this is happening because you are just logging the raw data. Try creating a string from the data and then logging it like this:
body = [[NSString stringWithFormat:#"geoX#%##geoY#%#", string1, string2] dataUsingEncoding:NSUTF8StringEncoding];
NSString *string = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(#"%#",string);
I need to get url string from NSString. I do the following:
NSString * str = [NSString stringWithString:#"i#gmail.com"];
NSString * eStr = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"%#",eStr);
The result is i#gmail.com. But I need i%40gmail.com. replacing NSUTF8StringEncoding with NSASCIIStringEncoding doesn't help.
You're using the wrong method. This does the opposite, translating percent
escapes to their characters. You probably want to use
stringByAddingPercentEscapesUsingEncoding:.
NSString *str = #"i#gmail.com";
NSString *eStr =
[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Apart from that, looks like the # character is not escaped by default. Then,
as the documentation for the above method points out, you'll need to use
CoreFoundation to achieve what you want.
NSString *str = #"i#gmail.com";
CFStringRef eStr = CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(CFStringRef)str,
NULL,
(CFStringRef)#"#",
kCFStringEncodingUTF8
);
NSLog(#"%#", eStr);
CFRelease(eStr);
Please check the documentation to know more about the function used and
how to make it fit your needs.
I have multiple NSStrings and i wish to merge them into one other, here is my code so far...
NSString *newURL = [_parameters objectForKey:#"url"];
NSString *emailBody = #"Hey!<br>I just snipped my long url with My Cool App for iPhone in just a few seconds!<p><b>"+newURL+#"</b></p>";
If you know the number of your existing strings, you can just concat them:
NSString* longString = [firstString stringByAppendingString:secondString];
or:
NSString* longString = [NSString stringWithFormat:#"A string: %#, a float: %1.2f", #"string", 31415.9265];
If you have an arbitrary number of strings, you could put them in an NSArray and join them with:
NSArray* chunks = ... get an array, say by splitting it;
NSString* string = [chunks componentsJoinedByString: #" :-) "];
(Taken from http://borkware.com/quickies/one?topic=NSString)
Another good resource for string handling in Cocoa is: "String Programming Guide"
You can try
NSString *emailBody = [ NSString stringWithFormat: #"Hey!<br>I just snipped my long url with Snippety Snip for iPhone in just a few seconds, why not check it out?<p><b>%#</b></p>", newURL ];
Given that you've got multiple strings I recommend using an Array:
NSArray *array = [NSArray arrayWithObjects:#"URL", #"person", "body"];
NSString *combined = [array componentsJoinedByString:#""];
Formatting string has better readability and less error-prone:
NSString *newURL = [_parameters objectForKey:#"url"];
NSString *emailBody = [NSString stringWithFormat:#"Hey!<br>I just snipped my long url with Snippety Snip for iPhone in just a few seconds, why not check it out?<p><b>%#</b></p>", newUrl, newUrl];
You can concatenate strings in Cocoa using:
[NSString stringByAppendingString:]
Or you could use the [NSString stringWithFormat] method which will allow you to specify a C-style format string with a variable argument list to populate the escape sequences.