Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I want to split a string into fixed sized chunks (say 140 characters each) in JavaScript using space as delimiter(i.e it should not break words), Note: It should handle newline character Currently I'm using wordwrap npm package, but It does not handle new-line character.

var wrap = require('wordwrap')(140)    
var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
console.log(wrap(str));

It works fine for normal text but if text contains new-line carriage I get following error:
SyntaxError: Unexpected token ILLEGAL
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

share|improve this question
    
please add some examples. – Nina Scholz 1 hour ago
    
Can you include string at Question? What should be size of "fixed sized chunks"? What have you tried? – guest271314 1 hour ago
    
@Mohan stackoverflow.com/help/how-to-ask – Gandhi 1 hour ago
    
please add the wanted output, too. – Nina Scholz 1 hour ago
1  
It works fine you should show example, that not works – vp_arth 1 hour ago
up vote 2 down vote accepted

// define string variable
var string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"

function sliceMyString(str){
// initialize array (not required but verbose)
    var slices = [];

// while string is not empty
// take 140 characters
// check witch one was the last space or if the end of the line is reached
// then => push them in slices
// then => remove them from the string

    while(str != ''){
        var lastSpace = 0;

        for(var i = 0; i < str.length && i < 140; i ++){
            if(str[i] == ' '){
                lastSpace = i;
            }
            if(i == str.length - 1){
                lastSpace = str.length;
            }
        }

// insert into array (including trailing space, see below the codeblock)
        slices.push(str.slice(0, lastSpace));
        str = str.slice(lastSpace);
    }

// just logging the variables in the slices array

    slices.map(function(slice){
        console.log(slice);
    });
}

sliceMyString(string);

If you want to remove the trailing space, you can use trim():

slices.push(str.slice(0, lastSpace).trim());
share|improve this answer
    
your code breaks words – vp_arth 1 hour ago
    
Hi @randy, It surely breaks string into 140 character long chunks but it splits a word, eg, word "since" is broken at the end as String-1 "... sin" String-2 "ce .." – Mohan 1 hour ago
    
@vp_arth ooh didnt see that just saw chunks of 140 chars :P work in progress – randy 1 hour ago
    
@Mohan updated code – randy 43 mins ago
    
@randy Thanks, It was useful. – Mohan 24 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.