簡體   English   中英

替換來自json請求目標c的雙引號

[英]Replacing Double quotes from json request objective c

卡在下面。

我將替換文本字段文本中的"" (雙引號)並在 json 請求參數中發送該文本,但下面的代碼不起作用

NSString * mString = textfield.text; // Input through ipad/iphone/simulator keypad.

[mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mString length])];.

但是如果我硬編碼NSString * mString = @"\\"test" ; // 通過 xcode 輸入

replaceOccurrencesOfString:正在工作。

是雙引號輸入編碼的問題嗎?

為什么來自 ipad/iphone/模擬器鍵盤的雙引號與 xcode 雙引號的工作方式不同。

謝謝。

默認情況下,iOS 鍵盤使用“智能”引號...當您打開帶引號的字符串部分時使用left double quotation mark ,關閉帶引號的部分時使用right double quotation mark

所以要去掉這些,你需要替換雙引號和左雙引號和右雙引號。

這是一個快速示例:

NSString *origString = _myTextField.text;
NSString *strippedString = [origString copy];

NSArray *replaceChars = @[@"”", @"“", @"\""];

for (NSString *c in replaceChars) {
    strippedString = [strippedString
                      stringByReplacingOccurrencesOfString:c
                      withString:@""];
}

NSLog(@"Orig: [%@] / Stripped: [%@]", origString, strippedString);

示例輸出:

Orig: [“test”] / Stripped: [test]

您必須仔細觀察,但您會看到左右雙引號。


編輯

更簡潔的方法:

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"”“\""];
NSString *strippedString = [origString stringByTrimmingCharactersInSet:set];
NSLog(@"Orig: [%@] / Stripped: [%@]", origString, strippedString);

但是,正如 Sulthan 所指出的,您可能需要考慮其他語言的標點符號。

暫無
暫無

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

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