如何从Swift上的String中删除引号?

时间:2016-05-23 07:07:07

标签: swift swift2

我正在尝试删除Swift String中的引号,例如:

“你好”

以便Swift String只是:

您好

6 个答案:

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