如何从Swift上的字符串中删除引号?

时间:2022-09-15 13:02:42

I am trying to remove quotes from Swift String something like:

我正试图从Swift字符串中删除引号,比如:

"Hello"

“你好”

so that the Swift String is just:

因此,Swift字符串就是:

Hello

你好

6 个解决方案

#1


14  

You can simply use

你可以简单地使用

Swift 1

斯威夫特1

var str: String = "\"Hello\""

print(str) // "Hello"

print(str.stringByReplacingOccurrencesOfString("\"", withString: "")) // Hello

Update for Swift 3 & 4

更新Swift 3和4。

print(str.replacingOccurrences(of: "\"", with: "")) // Hello

#2


7  

"\"Hello\"".stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\""))

#3


2  

str.stringByReplacingOccurrencesOfString("\"", withString: "")

#4


1  

//try like this

/ /这样的尝试

var strString = "\"Hello\""

strString = strString.stringByReplacingOccurrencesOfString("\"", withString: "")

#5


0  

you can use \ to remove Quotes from string

您可以使用\从字符串中删除引号

\" (double quote)

\”(双引号)

\' (single quote)

\ '(单引号)

example:

例子:

NSString *s = @"your String";
NSCharacterSet *newStr = [NSCharacterSet characterSetWithCharactersInString:@"/""];
s = [[s componentsSeparatedByCharactersInSet: newStr] componentsJoinedByString: @""];
NSLog(@"%@", s); 

#6


0  

In Swift 3:

在斯威夫特3:

let myString = "\"Hello\""
print(myString)  // "Hello"

let myNewString = myString.replacingOccurrences(of: "\"", with: "")
print(myNewString) // Hello

#1


14  

You can simply use

你可以简单地使用

Swift 1

斯威夫特1

var str: String = "\"Hello\""

print(str) // "Hello"

print(str.stringByReplacingOccurrencesOfString("\"", withString: "")) // Hello

Update for Swift 3 & 4

更新Swift 3和4。

print(str.replacingOccurrences(of: "\"", with: "")) // Hello

#2


7  

"\"Hello\"".stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\""))

#3


2  

str.stringByReplacingOccurrencesOfString("\"", withString: "")

#4


1  

//try like this

/ /这样的尝试

var strString = "\"Hello\""

strString = strString.stringByReplacingOccurrencesOfString("\"", withString: "")

#5


0  

you can use \ to remove Quotes from string

您可以使用\从字符串中删除引号

\" (double quote)

\”(双引号)

\' (single quote)

\ '(单引号)

example:

例子:

NSString *s = @"your String";
NSCharacterSet *newStr = [NSCharacterSet characterSetWithCharactersInString:@"/""];
s = [[s componentsSeparatedByCharactersInSet: newStr] componentsJoinedByString: @""];
NSLog(@"%@", s); 

#6


0  

In Swift 3:

在斯威夫特3:

let myString = "\"Hello\""
print(myString)  // "Hello"

let myNewString = myString.replacingOccurrences(of: "\"", with: "")
print(myNewString) // Hello