javascript - Create a newline for every x number of characters - Stack Overflow

admin2025-04-16  0

Say I have an arbitrary string that has x number of characters. How can I insert a newline char in the string for every y characters? I just want to wrap the string, so that this:

xageaojigeaogjieaogjieaogjoajgoeajgoeagjaegaogjeaovc

bees

xageaoj
igeaogj
ieaogji
eaogjoa
jgoeajg
oeagjae
gaogjea
ovc

if the line wrap count is 7. One way would be to loop over the string and then push to array, something like this:

const wrap = (count, str) => {
  const letters = [], ln = str.length;

  for(let i = 0; i < ln; i++){
    letters.push(str[i]);
    if(i%count === 0)
    letters.push('\n');
  }

  return letters.join('');
};

but is there a better/simpler way to do this?

Say I have an arbitrary string that has x number of characters. How can I insert a newline char in the string for every y characters? I just want to wrap the string, so that this:

xageaojigeaogjieaogjieaogjoajgoeajgoeagjaegaogjeaovc

bees

xageaoj
igeaogj
ieaogji
eaogjoa
jgoeajg
oeagjae
gaogjea
ovc

if the line wrap count is 7. One way would be to loop over the string and then push to array, something like this:

const wrap = (count, str) => {
  const letters = [], ln = str.length;

  for(let i = 0; i < ln; i++){
    letters.push(str[i]);
    if(i%count === 0)
    letters.push('\n');
  }

  return letters.join('');
};

but is there a better/simpler way to do this?

Share edited Aug 5, 2018 at 21:02 asked Aug 5, 2018 at 20:54 user5047085user5047085
Add a ment  | 

4 Answers 4

Reset to default 5

You might use a regular expression .replace:

const input = 'xageaojigeaogjieaogjieaogjoajgoeajgoeagjaegaogjeaovc';
console.log(input.replace(/.{8}/g, '$&\n'));

What this does is, every 8 characters are matched (with .{8}), and then '$&' in the second argument represents the whole substring that was matched. So, just tack on a \n to the end, and every 8 characters will be replaced with those 8 characters plus a newline. You can also insert matched groups like this, among other things - see the docs.

RegExp + Array.prototype.join version:

const wrap = (raw, n) => raw.match(new RegExp(`.{1,${n}}`, 'g')).join('\n')

const source = 'xageaojigeaogjieaogjieaogjoajgoeajgoeagjaegaogjeaovc'

console.log(wrap(source, 7))

A different approach could be to use String#slice with an indicator for the next index for slicing.

const wrap = (str, count) => {
    var letters = [],
        i = 0;

    while (i < str.length) {
        letters.push(str.slice(i,  i+= count));
    }
    return letters.join('\n');
};

console.log(wrap('xageaojigeaogjieaogjieaogjoajgoeajgoeagjaegaogjeaovc', 7));

You can use a regular expression to match 7 characters and replace them with the match followed by a new line

str.replace(/(.{7})/g, "$1\n")

or in your code

const wrap = (str, count) =>
    str.replace(new RegExp(`(.{${count}})`, "g"), "$1\n")
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744809216a268183.html

最新回复(0)