我正在尝试删除Swift String
中的引号,例如:
“你好”
以便Swift String
只是:
您好
答案 0 :(得分:20)
您只需使用
即可Swift 1
var str: String = "\"Hello\""
print(str) // "Hello"
print(str.stringByReplacingOccurrencesOfString("\"", withString: "")) // Hello
Swift 3&更新4 强>
print(str.replacingOccurrences(of: "\"", with: "")) // Hello
答案 1 :(得分:10)
"\"Hello\"".stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\""))
答案 2 :(得分:2)
==
答案 3 :(得分:1)
//试试这个
var strString = "\"Hello\""
strString = strString.stringByReplacingOccurrencesOfString("\"", withString: "")
答案 4 :(得分:1)
在Swift 3中:
let myString = "\"Hello\""
print(myString) // "Hello"
let myNewString = myString.replacingOccurrences(of: "\"", with: "")
print(myNewString) // Hello
答案 5 :(得分:0)
您可以使用\从字符串
中删除引号\" (双引号)
\' (单引号)
示例:
NSString *s = @"your String";
NSCharacterSet *newStr = [NSCharacterSet characterSetWithCharactersInString:@"/""];
s = [[s componentsSeparatedByCharactersInSet: newStr] componentsJoinedByString: @""];
NSLog(@"%@", s);