簡體   English   中英

替換NSMutableString中的“ \\ r \\ n”,從服務器收到的數據

[英]Replace “\r\n” in NSMutableString, Data received from Server

我通過執行以下代碼從服務器獲取數據:

- (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];


    }

有人可以幫忙嗎?

大概試試看:

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

**解決了服務器上的此問題,我認為這是最好的做法,因為服務器要更強大,並且C#在字符串處理方面非常出色

謝謝。**

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM