我正在创建一个聊天应用程序。但是,现在我在发送特殊字符“&”时遇到了一个问题。我正在使用php web-services发送和接收消息。发送“&”字符,然后当某个人收到这个字符时,应用程序崩溃了。
答案 0 :(得分:1)
在这里用它的代码替换那个字符如下...
当它在web服务中传递时,将其替换为如下所示..
yourString = [yourString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
然后当你想在那时接收它时用特殊字符替换..
yourString = [yourString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
在UITextView
或UITextField
<强>更新强>
if (string2 != nil) {
string1 = [NSString stringWithString:string2];
NSString *strTemp = [NSString stringWithString:string2];
NSLog(@"\n\n Temp String ==>> %@",strTemp);
}
<强>更新强>
NSString *yourString = @"Hello & How Are You?";
yourString = [yourString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSLog(@"\n\n String Here => %@",yourString);
将字符串发送到web-serive时请使用bellow strign ..
NSString *yourStringForWeb = @"Hello & How Are You?";
yourStringForWeb = [yourStringForWeb stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
并将此yourStringForWeb
传递给网络服务...
NSString *string = @"Hello & How Are You?";
string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
NSLog(@"\n\n ===>> Before ==>> %@",string);
NSString *string2 = string;
string2 = [string2 stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
string2 = [string2 stringByReplacingOccurrencesOfString:@"%26" withString:@"&"];
NSLog(@"\n\n ===>> After ==>> %@",string2);
OutPut =&gt;
===>> Before ==>> Hello%20%26%20How%20Are%20You?
===>> After ==>> Hello & How Are You?