What is the cleanest way to build multi-line strings?
See original GitHub issueAs 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:
- Created 5 years ago
- Comments:11 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Another example is putting the strings into an array and then joining:
Use ES6 template literals where strings are started and ended with ` symbol.
is equivalent to …
"hello \nworld"