Split string at space after certain number of characters in Javascript

后端 未结 4 741
醉梦人生
醉梦人生 2020-12-16 03:28

I am attempting to create a tool that takes an input text and splits it into chunks of text at a certain # of characters. However, I need to make sure it does not split the

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-16 04:06

    you could use a simple function like this:

    function split(string) {
      for(i=154; i>=0; i--) {
        if(string.charAt(i) == " ") {
          var newString1 = string.slice(0, i);
          var newString2 = string.slice(i);
        }
      }
    }
    

    Instead of assigning to separate strings you can always put them into an array if you'd like as well.

提交回复
热议问题