how to include "\(" and ")" in formatted string output

  1. 3 years ago

    Alan LeRoy

    22 Apr 2020 User since 2020

    I need a method to return a string in the phone number format "\(XXX\) - xxx - xxxx" where the input value is a string of 10 characters. The "\(" and the "\)" are required as the output string is embedded in a PDF file in a draw text command of Td().

    My tried methods are as follows

    func formatPhone(theData: String)
    {
    return "\(\( \(theData[0...2])\) - \(theData[3...5]) - \(theData[6...9]))"
    }

    I have also tried
    func formatPhone(theData: String)
    {
    return "\("+"\(theData[0...2])+"\) ) - \(theData[3...5]) - \(theData[6...9]))"
    }

    I have also tried
    func formatPhone(theData: String)
    {
    return "\("+theData[0...2]+"\) - "+theData[3...5]"+" - "+theData[6...9]
    }

    I have not found a way to include the "\(" or the "\)" in the formatted output as the gravity compiler thinks there is an error as "\(" or the next ")" have a special meaning when compiling. Any ideas how to do this would be most appreciated. It is probably simpler than I make it out to be!

    Thanks for your time in advance.

  2. marco

    22 Apr 2020 Administrator User since 2016

    Hello @Alan LeRoy I think that by using double backslashes you'll be able to solve.
    Something like:

    return "\\(\(theData[0...2])\) - \(theData[3...5]) - \(theData[6...9])"
  3. Alan LeRoy

    22 Apr 2020 User since 2020

    That works!

    Thank you Marco.

or Sign Up to reply!