return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>Splitting a string into multiple lines in javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Splitting a string into multiple lines in javascript - Stack Overflow

programmeradmin1浏览0评论

I'm trying to find a way to split long strings to multiple lines so what I'm doing is insert text into an image and if it gets too long it overflows, newlines work but it wouldn't be best idea to let user add the newlines and split it in code, so if i give it a limit it checks if its over limit split to two lines or i mean a newline \n between it, however that's easy but my problem is when it es that the second part is also over the limit then it should split it in to 3 newlines, how would you go implement that?

Examples

split("sometext", 5); // somet\next
split("Hello", 2); // he\nll\no

I'm trying to find a way to split long strings to multiple lines so what I'm doing is insert text into an image and if it gets too long it overflows, newlines work but it wouldn't be best idea to let user add the newlines and split it in code, so if i give it a limit it checks if its over limit split to two lines or i mean a newline \n between it, however that's easy but my problem is when it es that the second part is also over the limit then it should split it in to 3 newlines, how would you go implement that?

Examples

split("sometext", 5); // somet\next
split("Hello", 2); // he\nll\no
Share Improve this question asked Sep 18, 2018 at 23:20 Free TNTFree TNT 811 gold badge3 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Very straightforward answer to your question:

function customSplit(str, maxLength){
    if(str.length <= maxLength)
        return str;
    var reg = new RegExp(".{1," + maxLength + "}","g");
    var parts = str.match(reg);
    return parts.join('\n');
}

You need a function like the following:

function split(str, maxWidth) {
  const newLineStr = "\n";
  done = false;
  res = '';
  do {
    found = false;
    // Inserts new line at first whitespace of the line
    for (i = maxWidth - 1; i >= 0; i--) {
      if (testWhite(str.charAt(i))) {
        res = res + [str.slice(0, i), newLineStr].join('');
        str = str.slice(i + 1);
        found = true;
        break;
      }
    }
    // Inserts new line at maxWidth position, the word is too long to wrap
    if (!found) {
      res += [str.slice(0, maxWidth), newLineStr].join('');
      str = str.slice(maxWidth);
    }

    if (str.length < maxWidth)
      done = true;
  } while (!done);

  return res + str;
}

function testWhite(x) {
  const white = new RegExp(/^\s$/);
  return white.test(x.charAt(0));
};
console.log(split("sometext", 5));
console.log(split("Hello", 2));

https://j11y.io/snippets/wordwrap-for-javascript/

Wraps using specified limit on characters. :)

What kind of interface are you building? If it's a web interface, you should style the string on the front end, instead of modifying it on the data layer.

If it's a text based interface and you really need to do this, then you can get the first n characters while there is a non-empty string, then join with '\n'. Assuming you have underscore:

function split(str, n) {
  let numberOfSegments = Math.ceil(_.size(str) / n);
  let segments = _.map(_.range(numberOfSegments), 
                       segmentIndex => str.substr(segmentIndex * n, n));
  return segments.join('\n');
}
发布评论

评论列表(0)

  1. 暂无评论