0

我正在创建一个聊天应用程序。但是,现在我在发送特殊字符“&”时遇到了一个问题。我正在使用 php web-services 发送和接收消息。同时发送“&”字符,然后当有人收到这个字符时,应用程序崩溃了。

4

1 回答 1

1

这里用它的代码替换那个字符,如下所示......

当时在网络服务中传递它时,像下面这样替换它..

yourString = [yourString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

然后当你想收到它时用特殊字符替换..

    yourString = [yourString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];

显示后UITextViewUITextField

更新:

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);

在将字符串发送到网络服务器时,使用下面的字符串..

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);

输出 =>

 ===>> Before ==>> Hello%20%26%20How%20Are%20You?
 ===>> After ==>> Hello & How Are You?
于 2013-04-16T04:59:59.257 回答