Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(swift) remove "\" character from string?

I have string which contains few \ and I want to remove all of them. can anyone tell me how can I do that. I have already tried stringByReplacingOccurrencesOfString but it's not working out for me.

like image 230
Rahul Sonvane Avatar asked May 12 '15 06:05

Rahul Sonvane


People also ask

How do I remove a specific character from a string in Swift?

Swift String remove() The remove() method removes a character in the string at the specified position.

How do I remove the first character from a string in Swift 5?

The dropFirst() method removes the first character of the string.

How do I remove the last two characters from a string Swift?

To remove the last character from a string we need to use the removeLast() method in swift.


2 Answers

var str = "\\dagdahughuad\\dajughdaug"
var str2 = str.stringByReplacingOccurrencesOfString("\\", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
print(str2)

This will output

dagdahughuaddajughdaug
like image 67
Leo Avatar answered Oct 26 '22 19:10

Leo


Converted above code in to SWIFT 3

    var str = "\\dagdahughuad\\dajughdaug"
    var str2 = str.replacingOccurrences(of: "\\", with: "", options: 
    NSString.CompareOptions.literal, range: nil)
    print(str2)
like image 37
rohit90 Avatar answered Oct 26 '22 19:10

rohit90