在 JavaScript 中每 15 个字符后添加新行
Adding new line after every 15 characters in JavaScript
我已经使用以下方法进行更改,但在添加新行后我得到了额外的空间。
我使用了 trim(),但它使值变得毫无意义。
function addSpace() {
var columnValue = "ER Throttle Position ER Throttle Position ER Throt";
var result = "";
while (columnValue.length > 0) {
result += columnValue.substring(0, 15) + "\n";
columnValue = columnValue.substring(15);
}
columnValue = result;
return columnValue;
}
console.log(addSpace());
您是在谈论输出 Throt
最后一行的 space 吗?实际上那里没有更多的数据,但是如果你想让你的输入字符串重复以填充其余的 space,你需要重复它以便尽可能多地填充它。
ER Throttle Position
的基本字符串(以 space 结尾)的长度为 21 个字符。对于 15 行长度,重复基本字符串 5 次将导致 7 行重复文本填满整个宽度(除非您计算最后的 space):
const output = document.getElementsByTagName("output")[0];
function addNewLine(columnValue = "", position = 0) {
if (columnValue === "" || position === 0) {
return "";
}
// Replacing spaces with underscore for visual representation
columnValue = columnValue.replaceAll(" ", "_");
let result = "";
while (columnValue.length > 0) {
result += columnValue.substring(0, position) + "\n";
columnValue = columnValue.substring(position);
}
//columnValue = result;
return result;
}
function print(message = "") {
output.innerHTML += `<pre>${message}</pre>`;
}
print(addNewLine("ER Throttle Position ER Throttle Position ER Throt", 15));
print(addNewLine("ER Throttle Position ER Throttle Position ER Throttle Position ER Throttle Position ER Throttle Position ", 15));
pre {
border: 1px solid black;
max-width: 7.5rem;
padding: 0.5rem;
}
pre::before {
border-bottom: 2px solid green;
content: "0123456789abcde";
display: block;
margin-bottom: 0.5rem;
}
<output></output>
我的代码中所做的更改:
- 添加了两个参数来概括每
position
个字符添加新行的功能
- 添加了一行以显示 spaces 使用下划线
_
(可选)
- 在返回
columnValue
之前评论了 result
到 columnValue
的赋值
- 改为返回
result
我已经使用以下方法进行更改,但在添加新行后我得到了额外的空间。 我使用了 trim(),但它使值变得毫无意义。
function addSpace() {
var columnValue = "ER Throttle Position ER Throttle Position ER Throt";
var result = "";
while (columnValue.length > 0) {
result += columnValue.substring(0, 15) + "\n";
columnValue = columnValue.substring(15);
}
columnValue = result;
return columnValue;
}
console.log(addSpace());
您是在谈论输出 Throt
最后一行的 space 吗?实际上那里没有更多的数据,但是如果你想让你的输入字符串重复以填充其余的 space,你需要重复它以便尽可能多地填充它。
ER Throttle Position
的基本字符串(以 space 结尾)的长度为 21 个字符。对于 15 行长度,重复基本字符串 5 次将导致 7 行重复文本填满整个宽度(除非您计算最后的 space):
const output = document.getElementsByTagName("output")[0];
function addNewLine(columnValue = "", position = 0) {
if (columnValue === "" || position === 0) {
return "";
}
// Replacing spaces with underscore for visual representation
columnValue = columnValue.replaceAll(" ", "_");
let result = "";
while (columnValue.length > 0) {
result += columnValue.substring(0, position) + "\n";
columnValue = columnValue.substring(position);
}
//columnValue = result;
return result;
}
function print(message = "") {
output.innerHTML += `<pre>${message}</pre>`;
}
print(addNewLine("ER Throttle Position ER Throttle Position ER Throt", 15));
print(addNewLine("ER Throttle Position ER Throttle Position ER Throttle Position ER Throttle Position ER Throttle Position ", 15));
pre {
border: 1px solid black;
max-width: 7.5rem;
padding: 0.5rem;
}
pre::before {
border-bottom: 2px solid green;
content: "0123456789abcde";
display: block;
margin-bottom: 0.5rem;
}
<output></output>
我的代码中所做的更改:
- 添加了两个参数来概括每
position
个字符添加新行的功能 - 添加了一行以显示 spaces 使用下划线
_
(可选) - 在返回
columnValue
之前评论了 - 改为返回
result
result
到 columnValue
的赋值