gpt4 book ai didi

javascript - 将大字符串分成几部分

转载 作者:行者123 更新时间:2023-12-02 07:42:59 24 4
gpt4 key购买 nike

我有多个字符串如下 -

var str1 = "this is a test that goes on and on"+param1
var str2 = "this is also a test this is also"+param2+" a test this is also a test this is also a tes"
var str3 = "this is also a test"

我将每个字符串分配到它自己的 var 中,以保持代码的可读性并防止字符串值尾随字符串。另外,正如我所读到的,换行符 javascript 字符不适用于所有浏览器 - Creating multiline strings in JavaScript

然后我连接字符串 -

var concatStr = str1 + str2 + str3

并返回字符串连接值。

这是将大字符串分解成多个部分的可接受方法吗?还是可以改进?

最佳答案

无需将每一行分配给不同的变量:

var str1 = "this is a test that goes on and on"+param1 +
"this is also a test this is also"+param2+
" a test this is also a test this is also a tes" +
"this is also a test";

就我个人而言,我会执行以下操作:

var string = ['hello ', param1,
'some other very long string',
'and another.'].join('');

对我来说,打字和阅读更容易。

关于javascript - 将大字符串分成几部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9210046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com