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

iezvtpos  于 2022-11-06  发布在  Swift
关注(0)|答案(6)|浏览(208)

我正在尝试删除Swift String中的引用,例如:
“你好啊”
因此Swift String就相当于:
你好

ippsafx7

ippsafx71#

您只需使用

雨燕1

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

print(str) // "Hello"

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

Swift 3和4的更新

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

5lwkijsr2#

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

nnsrf1az3#

在斯威夫特3中:

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

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

n8ghc7c14#

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

ryoqjall5#

//这样尝试

var strString = "\"Hello\""

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

kmb7vmvb6#

您可以使用\从字符串中删除引号
\”(双引号)
'(单引号)
例如:

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

相关问题