.replacingOccurrences(of: "\"", with: "") not working

Despite seeing many examples of this method used to remove “ from String VariablesI can’t get it to work… Other characters work okay so the issue seems to be “\”” Can anyone help with this please.

inline-code let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: """, with: "")

        cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue inline-code

Answered by GMacD53 in 741323022
print("cards.firstCardFirstWordClue", cards.firstCardFirstWordClue)  // cards.firstCardFirstWordClue A “form” of greeting “ “””

        let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: "\"", with: "")
        cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue
        print("removeQuotesFirstCardFirstWordClue", cards.firstCardFirstWordClue)  // removeQuotesFirstCardFirstWordClue A “form” of greeting “ “””

        let removeCommasFirstCardSecondWordClue = cards.firstCardSecondWordClue.replacingOccurrences(of: ",", with: "")
        cards.firstCardSecondWordClue = removeCommasFirstCardSecondWordClue
        print("removeCommasFirstCardSecondWordClue", cards.firstCardSecondWordClue)

Can you show the code or an example that does not work ?

I tested the following:

var testString = "hello \"GMacD53\""
print("With quotes:", testString)
testString = testString.replacingOccurrences(of: "\"", with: "")
print("Without quotes:", testString)

it works:

With quotes: hello "GMacD53"
Without quotes: hello GMacD53

Firstly, thanks once again for taking your time to help me. Unfortunately I won’t be able to show the code for a while (on the road with an iPhone 6). The difference I see between your example and what I am trying to do is, I am trying to remove any accidental stray “ and/or a deliberate “any text“ that maybe put in a Form TextField. I know I tested the stray “ and “””” and they didn’t work but I am pretty sure I didn’t try a normal input like your example. When I am able I will check all three examples and report back. Thanks again and a Happy New Year to you and yours.

let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: "\"", with: "")

        cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue

let removeCommasFirstCardSecondWordClue = cards.firstCardSecondWordClue.replacingOccurrences(of: ",", with: "")

        cards.firstCardSecondWordClue = removeCommasFirstCardSecondWordClue

“\”” doesn’t work on the TextField input cards.firstCardFirstWordClue but "," does.

Maybe I need to prevent commas and quotes from being typed into the TextField if that is possible?

So the text is something typed in a TextField ? Could you show what was the initial value of cards.firstCardFirstWordClue before replacingOccurrences ?

.

Maybe I need to prevent commas and quotes from being typed into the TextField if that is possible?

Yes it is possible. Use

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { }
let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: "\"", with: "")

        cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue

        print(cards.firstCardFirstWordClue)

        let removeCommasFirstCardSecondWordClue = cards.firstCardSecondWordClue.replacingOccurrences(of: ",", with: "")

        cards.firstCardSecondWordClue = removeCommasFirstCardSecondWordClue

        print(cards.firstCardSecondWordClue)

TextField Input (cards.firstCardFirstWordClue) was… A “form” of greeting

Print (cards.firstCardFirstWordClue) result was… A “form” of greeting (quotes still enclosing the word form)

TextField Input (cards.firstCardSecondWordClue) was… Another word for, this planet

Print (cards.firstCardSecondWordClue) result was… Another word for this planet (comma removed after the word for)

After much experimenting I have concluded that despite of: """, with: "") working okay on a string as per your example it doesn’t work on a string constructed from a TextField. I will try working on this at the TextField input level.

Thanks again for your help.

Please show the exact results. It is just impossible to guess what you mean.Show the results of the print.

        print("cards.firstCardFirstWordClue", cards.firstCardFirstWordClue)  // <<-- ADD THIS ONE

        let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: "\"", with: "")
        cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue
        print("removeQuotesFirstCardFirstWordClue", cards.firstCardFirstWordClue)  // <<--  ADD A LABEL to make it clear

        let removeCommasFirstCardSecondWordClue = cards.firstCardSecondWordClue.replacingOccurrences(of: ",", with: "")
        cards.firstCardSecondWordClue = removeCommasFirstCardSecondWordClue
        print("removeCommasFirstCardSecondWordClue", cards.firstCardSecondWordClue)

Accepted Answer
print("cards.firstCardFirstWordClue", cards.firstCardFirstWordClue)  // cards.firstCardFirstWordClue A “form” of greeting “ “””

        let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: "\"", with: "")
        cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue
        print("removeQuotesFirstCardFirstWordClue", cards.firstCardFirstWordClue)  // removeQuotesFirstCardFirstWordClue A “form” of greeting “ “””

        let removeCommasFirstCardSecondWordClue = cards.firstCardSecondWordClue.replacingOccurrences(of: ",", with: "")
        cards.firstCardSecondWordClue = removeCommasFirstCardSecondWordClue
        print("removeCommasFirstCardSecondWordClue", cards.firstCardSecondWordClue)

.replacingOccurrences(of: "\"", with: "") not working
 
 
Q