Insert CSV line break every N characters

Asked

Viewed 1,245 times

1

I have a js code that has a var that takes a string and this string needs to be broken every n lines so as not to break the maximum width of csv content. Besides, no word needs to be broken in half.

What would be the best solution for this? Considering the following example:

var n = 50;
var str = "Testando um algoritmo para quebrar esse texto a cada n caracteres sem haver quebra de palavras";

2 answers

0


Hello friend searching on the internet found this function:

function wordwrap( str, width, brk, cut ) {

    brk = brk || '\n';
    width = width || 75;
    cut = cut || false;

    if (!str) { return str; }

    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');

     return str.match( RegExp(regex, 'g') ).join( brk );
}

Use:

wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br/>\n');

Upshot:
The Quick Brown fox
jumped over the Lazy
dog.

Function with start and end concatenation:

  function wordwrap( str, width, brk) {

    brk = brk || '\n';
    width = width || 75;

    if (!str) { return str; }

    var regex = '.{1,' +width+ '}(\\s|$)' + '|\\S+?(\\s|$)';

    var array = str.match( RegExp(regex, 'g') );

    var frase = "";

    for (var i = 0; i < array.length; i++) {
        console.log(array[i]);

        frase += i + " " + array[i] + "\n";
    };

    return frase;
   }

alert(wordwrap('The quick brown fox jumped over the lazy dog.', 30));
  • Excellent, Matheus. It was a regex so needed. Thank you.

  • Do you have any idea how I could insert a string at the beginning of each row of that? At the end is easy from the cut parameter, but I did not see a quick way to concatenate in front.

  • I made a modification in the function I will publish just below, if you can give me more points so I thank you =D

  • Friend, I modified the answer by adding the function I modified to concatenate at the beginning and at the end

  • Thanks, Matheus, for this I ended up applying something like this: <tag>' + wordwrap(phrase, 130, '</tag> n<tag>') + '</tag>'; But your solution is also great. Thanks again.

0

You can use the following function which reproduces the functioning of the wordwrap of PHP.

function wordwrap( str, width, brk, cut ) {

    brk = brk || '\n';
    width = width || 75;
    cut = cut || false;

    if (!str) { return str; }

    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');

    return str.match( RegExp(regex, 'g') ).join( brk );

}

Use:

function wordwrap( str, width, brk, cut ) {
 
    brk = brk || '\n';
    width = width || 75;
    cut = cut || false;
 
    if (!str) { return str; }
 
    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
 
    return str.match( RegExp(regex, 'g') ).join( brk );
 
}

var n = 50;
var str = "Testando um algoritmo para quebrar esse texto a cada n caracteres sem haver quebra de palavras";

var nova = wordwrap(str, n, "\n");
alert(nova);

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.