skip to Main Content

I had string text which I wanted to break up. I needed to send it across a Facebook messenger chat API but that API only allows 640 characters and my text is much longer. I wanted a neat solution with which I could send the text across.

I was thing of splitting that string which contains multiple paragraphs in to two halves to the nearest full stop.

Eg

var str = "This is paragraph one. I need Mr. Sam to my errand. The errand must be done by him. He is the best.

Mr. Sharma is also good. Btw this is the second paragraph. I think you get my point. Sentence again. Sentence again.

Paragraph three is started. I am writing so much now. This is fun. Thanks";

//Expected output

var half1 = "This is paragraph one. I need Mr. Sam to my errand. The errand must be done by him. He is the best.

Mr. Sharma is also good. Btw this is the second paragraph."

var half2 = "I think you get my point. Sentence again. Sentence again.

Paragraph three is started. I am writing so much now. This is fun. Thanks"

3

Answers


  1. var results=[];
    var start=0;
    for(var i=640;i<str.length;i+=640){//jump to max
     while(str[i]!=="."&&i) i--;//go back to .
      if(start===i) throw new Error("impossible str!");
      results.push(str.substr(start,i-start));//substr to result
      start=i+1;//set next start
     }
    }
    //add last one
    results.push(str.substr(start));
    

    You could walk over the string by making a 640 step forward, then going back to the last . , create a substring and repeat.

    Login or Signup to reply.
  2. Consider this as a base:

    let slices = [], s;
    for(let i = 0; s = a.slice(i * 640, (i+1) *  640); i++)
        slices.push(s);
    

    The slices array will contain 640 char chunks of your text. But we want this to be space aware. We need to find the end of a sentence as close to that 640 mark as possible without going past it. If we want to be space aware, it’ll make our lives easier to deal with whole sentences rather than characters:

    // EDIT: Now, if a sentence is less than 640 chars, it will be stored as a whole in sentences
    // Otherwise, it will be stored in sequential 640-char chunks with the last chunk being up to 640 chars and containing the period.
    // This tweak also fixes the periods being stripped
    let sentences = str.match(/([^.]{0,639}.|[^.]{0,640})/g)
    

    Here is a quick demo of that nasty regex in action: https://jsfiddle.net/w6dh8x7r

    Now we can create our result up to 640 characters at a time.

    let result = ''
    sentences.forEach(sentence=> {
        if((result + sentence).length <= 640) result += sentence;
        else {
            API.send(result);
            // EDIT: realized sentence would be skipped so changed '' to sentence
            result = sentence;
        }
    })
    
    Login or Signup to reply.
  3.     var txt = 'This is paragraph one. I need Mr. Sam to my errand. The errand must be done by him. He is the best. Mr. Sharma is also good. Btw this is the second paragraph. I think you get my point. Sentence again. Sentence again. Paragraph three is started. I am writing so much now. This is fun. Thanks'
        var p = []
        splitValue(txt, index);
    
        function splitValue(text)
        {
            var paragraphlength = 40;
            if (text.length > paragraphlength) 
            {
                var newtext = text.substring(0, paragraphlength);
                p.push(newtext)
                splitValue(text.substring(paragraphlength + 1, text.length))
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search