且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IOS 将 URL 字符串转换为 NSString?

更新时间:2023-02-23 09:15:59

您的 urlString 中是 Unicode 汉字,这就是它不转换的原因.

It is Unicode han characters in your urlString thats why it is not converting.

将 %u 替换为 \u,您将获得字符串.

Replace %u to \u and you will get your String.

NSString *str=@"%3CTEXTFORMAT%20LEADING%3D%222%22%3E%3CP%20ALIGN%3D%22LEFT%22%3E%3CFONT%20FACE %3D%22Arial%22%20SIZE%3D%2212%22%20COLOR%3D%22%23000000%22%20LETTERSPACING%3D%220%22%20KERNING%3D%220%22%3E%u53F0%u5317%u7E2323141%u65B0%u5E97%u6C11%u6B0A%u8DEF130%u5DF714%u865F5%u6A13%3C/FONT%3E%3C/P%3E%3C/TEXTFORMAT%3E";
str=[str stringByReplacingOccurrencesOfString:@"%u" withString:@"\\u"];
NSString *convertedStr=[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"converted string is %@ \n",convertedStr);

输出:---------------

output :---------------

converted string is <TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">\u53F0\u5317\u7E2323141\u65B0\u5E97\u6C11\u6B0A\u8DEF130\u5DF714\u865F5\u6A13</FONT></P></TEXTFORMAT>

有关更多信息,请关注此url
这是中文unicode字符这里有一些代码可以证明这一点:

for more Info follow this url
This is chinese unicode char here is some code that will prove it:

NSString *newStr=@"\u53F0\u5317\u7E2323141\u65B0\u5E97\u6C11\u6B0A\u8DEF130\u5DF714\u865F5\u6A13";
NSLog(@"chinese string is %@",[newStr stringByReplacingPercentEscapesUsingEncoding:NSUTF16StringEncoding]);

输出:----------------------台北县23141新店民权路130巷14号5楼

output:---------------------- 台北縣23141新店民權路130巷14號5樓

去谷歌翻译转换这个字符串会给你某人的地址.如:-

go to google translate converting this string will give you someone's address. as :-

市民台北县新店23141路130号14弄5楼