首页 > 解决方案 > 如何将一个字符串附加到另一个具有多行并由反斜杠字符终止的字符串

问题描述

我想将字符串变量附加到另一个字符串。第二个字符串包含大量字符,多行并由反斜杠字符(“\”)分隔。

我正在寻找 javascript 中的解决方案

例如

var one = 'test string one'; 

var two = 'test string two\
              is seperated by back slash and this string has\
              multiple number of lines';

现在我想将字符串一附加到字符串二,如下所示

var two = 'test string two'+one+\
              is seperated by back slash and this string has\
              multiple number of lines';

我正在寻找用第二个字符串附加第一个字符串。任何帮助,将不胜感激。

标签: javascriptstring

解决方案


您必须使用 a 再次开始字符串文字'

'test string two' + one + '\
 is seperated by back slash and this string has\
 multiple number of lines';

或者您使用模板字符串,那么您根本不需要换行符:

`test string two ${one}
 is seperated by back slash and this string has
 multiple number of lines`

推荐阅读