How to remove quotes from a string in Swift?

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

"Hello"

to make Swift String simple:

Hello

+6
source share
6 answers

You can just use

Swift 1

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

Update for Swift 3 and 4

 print(str.replacingOccurrences(of: "\"", with: "")) // Hello 
+11
source
 "\"Hello\"".stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\"")) 
+7
source
 str.stringByReplacingOccurrencesOfString("\"", withString: "") 
+2
source

// try to do it

 var strString = "\"Hello\"" strString = strString.stringByReplacingOccurrencesOfString("\"", withString: "") 
+1
source

you can use \ to remove quotes from a string

\ "(double quote)

\ '(single quote)

example:

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

In Swift 3:

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

All Articles