如何创建javascript forloop
,将输入作为常规字符串,但以固定间隔插入\n
?
例如,如果这是输入:
输入:
x="This is a new line.This is a new line.This is a new line.This is a new line.This is a new line."
假设间隔设置为19
循环将在整个字符串
的19个字符的末尾插入\n
x="This is a new line.**\n**This is a new line.**\n**This is a new line.**\n**This is a new line.**\n**This is a new line."
从而产生输出,如果我们要做alert(x)
This is a new line.
This is a new line.
This is a new line.
This is a new line.
This is a new line.
我已经尝试了几个小提琴,但没有一个可以达到合适的结果。
类似的问题并没有完全回答我的问题:
答案 0 :(得分:1)
var x = x="This is a new line.This is a new line.This is a new line.This is a new line.This is a new line."
var a = [];
var i = 12; // Every 12 characters
// Split the string into an array of 12 character strings
do {
a.push(x.substring(0, i));
} while((x = x.substring(i, x.length)) != "");
// Join the strings
a = a.join('\n');
alert(a);