简体   繁体   中英

Add line break in string on last character before n characters


To start off, this is probably worded badly, as I am not sure how to put what I want into words
Lets say I have this canvas (I'm using node-canvas ) and I want to make it display text from a user input. However, the way I am doing it limits the number of characters to 36-38 (not looking for a solution to this). So I made a script using the Regex textstr.match(/.{1,32}/g) that splits the string every 32 characters (just to be safe), calculates a new canvas height, and then does join("\\n") when it comes time to print the string. However, when receiving feedback on this, I realized it would be better to split along the last space in the string and add a line break there, but I am confused how to do this.
My current code is this:

textStr = "123456789 01234567890 123456789012 34567890"
var splitStr 
    if(textstr.length > 32){
    if(textstr.substring(1,32).includes(" ")){ //1,32 so it won't bug out if the first character is a space
//splitStr = textstr.something(test)
    
    } else  {
      splitStr = textstr.match(/.{1,32}/g)
      
    }
    } 
    //canvas initialization blah blah blah
    //load fonts yada yada yada
    ctx.fillText(splitStr.join("\n"), 20, 55) 

I was wondering if there was some sort of regex expression that I could use. Any help/feedback/common sense is appreciated

This solution is a bit complex and can due with some simplification. However it should get you mostly there.

const input = "123456789 01234567890 123456789012 34567890 11444444444 424124  1234124124121 4444444444444444444444444444444444444444444444444444444444444444444444";

const split = (value, width) => {
  const stack = value.split(' ').reverse();
  const results = [];
  let builder = "";
  
  while (stack.length > 0) {
    const item = stack.pop();
    
    if (item.length > width) { // is the current chunk already larger than  our desired width?
      if (builder !== "") { // we have to push our buffer too
        results.push(builder);
        builder = "";
      }
        
      results.push(item);
    } else {
      const line = builder === ""
        ?   item
        : `${builder} ${item}`;
        
      if (line.length > width) { // is our new line greater than our width?
        stack.push(item); // push the item back, since consuming it would make our line length too long. we let the next iteration consume it.              results.push(builder); // push the buffer into our results.
        builder = "";
      } else if (stack.length === 0) { // is this the last element? just add it to the results.
        results.push(line);
      } else {
        builder = line; // update our buffer to the current appended chunk.
      }
    }
  }
  
  return results;
};

split(input, 32).forEach((c) => console.log(c, c.length));
split(input, 32).join("\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM