Ansal Antony
Ansal Antony

Reputation: 693

How can I remove quotes from String on Swift?

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

"Hello"

so that the Swift String is just:

Hello

Upvotes: 18

Views: 17526

Answers (6)

N J
N J

Reputation: 27505

You can simply use

Swift 1

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

print(str) // "Hello"

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

Update for Swift 3 & 4

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

Upvotes: 29

Marcos Reboucas
Marcos Reboucas

Reputation: 3489

In Swift 3:

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

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

Upvotes: 4

user5938635
user5938635

Reputation:

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

Upvotes: 0

Satyanarayana
Satyanarayana

Reputation: 1067

//try like this

var strString = "\"Hello\""

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

Upvotes: 1

Rahul Shirphule
Rahul Shirphule

Reputation: 999

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

Upvotes: 2

Losiowaty
Losiowaty

Reputation: 8006

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

Upvotes: 14

Related Questions