printjs分页打印字符串
时间: 2023-07-30 14:07:06 浏览: 106
你可以使用以下代码实现打印分页字符串:
```javascript
function printStringWithPagination(str, pageSize) {
const lines = str.split('\n');
const numLines = lines.length;
let pageNum = 1;
let lineNum = 0;
let output = '';
while (lineNum < numLines) {
output += `Page ${pageNum}\n\n`;
for (let i = 0; i < pageSize && lineNum < numLines; i++) {
output += `${lines[lineNum++]}\n`;
}
pageNum++;
}
console.log(output);
}
// Example usage
const myString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at tellus auctor, faucibus lacus a, euismod tortor. Ut eget lobortis nisl. Nulla facilisi. Aliquam erat volutpat. Proin commodo interdum nibh, sed venenatis mi vehicula et. Cras in pretium nulla. Cras vitae arcu non mauris interdum ultrices.';
printStringWithPagination(myString, 5);
```
这个函数将打印一个包含给定字符串的分页输出,每页包含给定行数。在上面的示例中,我们将 `myString` 字符串分成每页 5 行,并打印每一页的页码和行。
阅读全文