Home > Back-end >  How to format a very large string to send on whatsapp
How to format a very large string to send on whatsapp

Time:07-21

I have a very large string of text. I want to add spaces, break line, etc to send a message on WhatsApp. How can I do this? My text will be even longer than the string below. You know those well-formatted automatic messages you get on WhatsApp? I need to do something similar.

const bodyMessage = `Hello Mr. Responsible! I'm here to tell you your child's routine on ${date}. His/her entry was at ${entry} and his/her exit at ${exit}. I will tell you how the student's meals were on the day! The student's meals occurred as follows: collation: ${collation}, lunch: ${lunch}, snack: ${snack}, dinner: ${dinner}. ${descriptionRemedy}`

CodePudding user response:

As you are already using backticks in your string, you can add newlines in the message by including newlines in your string.

For example:

const bodyMessage = `Hello Mr. Responsible!

I'm here to tell you your child's routine on ${date}.

His/her entry was at ${entry} and his/her exit at ${exit}.

I will tell you how the student's meals were on the day! The student's meals occurred as follows:
collation: ${collation}
lunch: ${lunch}
snack: ${snack}
dinner: ${dinner}

${descriptionRemedy}`;

And this will render like:

enter image description here

You can also use the newline character \n like this:

const bodyMessage = `Hello Mr. Responsible!\n\nI'm here to tell you your child's routine on ${date}.\n\nHis/her entry was at ${entry} and his/her exit at ${exit}.\n\nI will tell you how the student's meals were on the day! The student's meals occurred as follows:\ncollation: ${collation}\nlunch: ${lunch}\nsnack: ${snack}\ndinner: ${dinner}\n\n${descriptionRemedy}`;

But this is harder to read.

CodePudding user response:

Try this approach (\n line break works for WhatsApp too)

How do I Add a Line Break in my SMS or MMS Message?

Formatting WhatsApp Message Text

  • Related