How do I add a line break in my Twilio SMS message?

15,866

Solution 1

If you are using an official Twilio library, the proper syntax is \n.

In your case, this should work:

var body = "Hello from Govind Singh Nagarkoti! \n Your verification code is " + code

Solution 2

Just a tip to anyone else who tries to use \n to take the content to a new line: the "\n" has to be within double quotes; if it's contained within single quotes it will print out as '\n'.

Solution 3

Use %0a , it might be helpful.

Reference :http://www.twilio.com/help/faq/sms/how-do-i-add-a-line-break-in-my-sms-message

Solution 4

You can either use \n with single quotes

val body = s"Hello from Govind Singh Nagarkoti!\nYour verification code is $code"

or a new line with triple quotes:

var body = s"""Hello from Govind Singh Nagarkoti! 
Your verification code is $code"""

Note that I also used string interpolation here by prefixing the variable with $ and prefixing the string with s.

Solution 5

Make sure your IDE doesn't automatcally add another \ if you copy pasted "\n". This is what tripped me. I pasted "\n" and it got inserted as "\\n" so naturaly the first "\" made the second "\" the special character and resulted in the "n" being treated as a simple "n" character. Hope this helps.

Share:
15,866
Govind Singh
Author by

Govind Singh

Making stuff work... most of the time.

Updated on June 05, 2022

Comments

  • Govind Singh
    Govind Singh almost 2 years

    Using Java/Scala. I am sending this string (an sms message) to a user mobile number by a Twilio Account.

    var body = "Hello from Govind Singh Nagarkoti! Your verification code is " + code
    

    This goes out to the user mobile, in 1 line.

    I want a newline after the first sentence. I want the user to receive:

    "Hello from Govind Singh Nagarkoti!
     Your verification code is 240190" 
    

    How can I enter a line break?

  • Govind Singh
    Govind Singh almost 10 years
    user getting as it is message with %0a
  • Amar
    Amar almost 10 years
    As far I understood curl -X is a command line client tool for URL manipulations,it meant making a call to the REST API given with the sets of data.U might try with \n once