question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the cleanest way to build multi-line strings?

See original GitHub issue

As the title mentions, what do you all think is the cleanest way to build an multi-line string.

For example, what if I want to define a function that builds a multiline string that looks like the following:

Your groups:
Group1
Group2

Here are examples of ways I was considering accomplishing this: (The function in the example is necessary to show how strange the indentation in code will look for template strings)

function doSomething() {
  const text = 'Your groups:'
    + '\nGroup1'
    + '\nGroup2';
  return text;
}

vs

// Subsequent lines in the string have no indent in code because we don't want indents in the string
function doSomething() {
  const text = `Your groups:
Group1
Group2`;
  return text;
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
chrisngobanhcommented, Aug 1, 2018

Another example is putting the strings into an array and then joining:

function doSomething() {
  const text = ['Your groups', 'Group1', 'Group2'].join('\n');
  return text;
}
6reactions
shubhamAsolkar22commented, Nov 28, 2018

Use ES6 template literals where strings are started and ended with ` symbol.

`hello 
world`

is equivalent to … "hello \nworld"

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the cleanest way to write a multiline string in JavaScript?
Almost identical to NickFitz's answer: var str = ["" ,"line 1" ,"line 2" ,"line 3" ].join(""); // str will contain "line1line2line3".
Read more >
JavaScript Multiline String – How to Create Multi Line Strings ...
In this article, you will learn three different ways to create multi-line strings in JavaScript. I will first explain the basics of strings ......
Read more >
Text blocks in Java. A better way to create multi-line…
We can use String methods like concat, join or use libraries like StringBuilder, Guava Joiner to make it a bit cleaner.
Read more >
1.2. Creating Multiline Strings - Scala Cookbook [Book] - O'Reilly
A cleaner approach is to add the stripMargin method to the end of your multiline string and begin all lines after the first...
Read more >
How to Create Multiline Strings in Go - freshman.tech
This is where raw string literals come in handy. They are enclosed in back ticks and are displayed exactly as they are written...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found