替换“\r\n”在 NSMutableString 中,从服务器接收的数据

发布于 2025-01-06 12:46:18 字数 1899 浏览 0 评论 0原文

我通过执行以下代码从服务器获取数据:

- (void) sendGeneral:(NSString *) general{

    self.responseData = [NSMutableData data];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:CBURL]];

    [request setHTTPMethod:@"POST"];
    int i;
    i = [general length];
    NSLog(@"Length is %i", i);

    [request setHTTPBody:[general dataUsingEncoding:NSUTF8StringEncoding]];

      [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

将其 NSLog 到包含“\r\n”的控制台后得到的数据,正如您从以下数据中看到的那样(仅演示了部分数据)

"numberPhoneFrom":"","FormName":"הרשמה\r\n"},{"CallID":314358,"StartDate":"27/07/2011 19:32","TotalTime":51.0,"numberPhoneFrom":"","FormName":"פרטים כלליים\r\n"},{"CallID":424378,"StartDate":"26/09/2011 18:43","TotalTime":63.6,"numberPhoneFrom":"","FormName":"הרשמה\r\n"},{"CallID":311229,"StartDate":"26/07/2011 13:28","TotalTime":18.6,"numberPhoneFrom":"","FormName":"נסיון\r\n"}

我正在尝试清理“\r\n”与各种命令,但我不能这样做这个代码获取协议中的数据

- (void) GetData: (NSMutableString *)protocol{
     [protocol replaceOccurrencesOfString:@"\n"  withString:@"" options:0 range:NSMakeRange(0, [protocol length])]; 
      NSLog(@"server data Function%@", protocol);
} 

从服务器返回数据在 NSMutableString 中听到

/*****************************************************************/
/* connection finish loading and continue talk                   */
/*****************************************************************/

    - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        [connection release];

       NSMutableString* responseString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];


      //  NSLog(@"the Server Response was %@", responseString);
         [[self delegate] GetData:responseString];


    }

有人可以帮忙吗?

Im getting data from a server by executing this code:

- (void) sendGeneral:(NSString *) general{

    self.responseData = [NSMutableData data];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:CBURL]];

    [request setHTTPMethod:@"POST"];
    int i;
    i = [general length];
    NSLog(@"Length is %i", i);

    [request setHTTPBody:[general dataUsingEncoding:NSUTF8StringEncoding]];

      [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

the data I'm getting after NSLog it to console containing "\r\n" as you can see from the following data (only part of the data to demonstration)

"numberPhoneFrom":"","FormName":"הרשמה\r\n"},{"CallID":314358,"StartDate":"27/07/2011 19:32","TotalTime":51.0,"numberPhoneFrom":"","FormName":"פרטים כלליים\r\n"},{"CallID":424378,"StartDate":"26/09/2011 18:43","TotalTime":63.6,"numberPhoneFrom":"","FormName":"הרשמה\r\n"},{"CallID":311229,"StartDate":"26/07/2011 13:28","TotalTime":18.6,"numberPhoneFrom":"","FormName":"נסיון\r\n"}

Im trying to clean the "\r\n" with all kind of commands but i can't do it this code get the data in the protocol

- (void) GetData: (NSMutableString *)protocol{
     [protocol replaceOccurrencesOfString:@"\n"  withString:@"" options:0 range:NSMakeRange(0, [protocol length])]; 
      NSLog(@"server data Function%@", protocol);
} 

Getting the data back from server in hear in NSMutableString

/*****************************************************************/
/* connection finish loading and continue talk                   */
/*****************************************************************/

    - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        [connection release];

       NSMutableString* responseString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];


      //  NSLog(@"the Server Response was %@", responseString);
         [[self delegate] GetData:responseString];


    }

can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亽野灬性zι浪 2025-01-13 12:46:18

大概可以试试这个:

[protocol replaceOccurrencesOfString:@"\\r\\n"  withString:@"" options:0 range:NSMakeRange(0, [protocol length])]; 

Probably try this:

[protocol replaceOccurrencesOfString:@"\\r\\n"  withString:@"" options:0 range:NSMakeRange(0, [protocol length])]; 
北陌 2025-01-13 12:46:18

**在服务器上解决了这个问题,我认为这是最好的办法'服务器更强大,C# 在字符串处理方面非常好,

谢谢。**

**Solved this issue on the server, i think this is the best thing to do' servers are much stronger and C# is very good in string handeling

Thank you.**

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文