-1

In Xcode with swift, how can I save text with quotation marks in it from a UItextfield into a string variable. I know how to use escape in a string "\"", but can't find a way to replace it from user input text field?

I am experimenting with encryption algorithms. I want my function to be able to take any type of user input, but keep hitting an error when my program tries to deal with quotation marks from the user input field at run time. I was hoping that a search replace type function might exist that would take user input and replace " with /". The documentation talks about escape sequences in relation to a string in the program that exists prior to running the program not for text that doesn't exist until the user types it in and hits the encrypt button.

I tried the suggestion below and:

var str = "I said \"Hello Everybody\"..."

var newstr = str.stringByReplacingOccurrencesOfString("\"", withString: "")

This would work, but the text coming in from the User interface is unescaped like this...

I said "Hello Everybody"

When I try the code above on the text from the text field, it will not find and replace the quote.

It lacks the escape characters, and even if I were to put them in the UIText as a user, I get an error when trying to replace the quotes. It works for a string, but not for user input...I can find and replace any other type of character from the UITextField, but I keep hitting a snag when it comes to these quotes.

I would really like to find a way of replacing the unescaped quote with an escaped one as below, but can't because the """ is in itself unescaped.

var newstr = str.stringByReplacingOccurrencesOfString(""", withString: "/"")
  • You need to provide some code and example strings. Also why it is a problem if a user enters quotes. What about non-ASCII alphabets and emoji? – zaph Jul 12 '15 at 00:57
  • Since the text is going into a string variable, it throws a quotation mark into the middle of a string. This causes the full string not to be parsed as a string. It is really just the quotes that are a problem. I will put some code up... Thanks for your reply though. I appreciate it. – user1676579 Jul 12 '15 at 01:20
  • Yes it is a confusing question ranging from parsing ti encryption without an explanation of what is the problem, what is the to be accomplished. – zaph Jul 12 '15 at 02:15

2 Answers2

0

I am not sure if I understand you correctly, an example would have been nice. You can use a string function to replace the quotation marks or add them.

var yourString = "Hello"
var newString = yourString.stringByReplacingOccurencesOfString("e",   withString: "a")

newString = "Hallo"

That also works with quotation marks So, as I said, not sure if that is what you were looking for...

Armin Scheithauer
  • 601
  • 1
  • 7
  • 17
0

Well there are no of ways of saving text from textfield within quotes into string.

    //I already set text = hi in the UITextfield
    @IBOutlet weak var text: UITextField!
    override func viewDidLoad() {
    super.viewDidLoad()

    //1st way
    let s = "\""+text.text+"\""
    println(s)



    //2nd way
    let t = "\"\(text.text)\""
    println(t)
    }

Output image:
enter image description here

Baig
  • 4,737
  • 1
  • 32
  • 47
Talha Q
  • 4,350
  • 4
  • 28
  • 40