简体   繁体   English

我如何分割一个字符串,以最大长度n断开?

[英]How do I split a string, breaking at a limit n length?

I have this string 我有这串

It's important to remember that this function does NOT replace newlines 
with <br> tags. Rather, it inserts a <br> tag before each newline, but 
it still preserves the newlines themselves! This caused problems for me 
regarding a function I was writing -- I forgot the newlines were still 
being preserved. 

Using JavaScript, what is the fastest way to parse this into 使用JavaScript,将其解析为最快的方法是

var string1 = "It's important to remember that this function does ...";

This means, I want limit string that has n length and in the end it has '...'. 这意味着,我希望限制字符串的长度为n,最后使用“ ...”。

Help me. 帮我。

Try this: 尝试这个:

var shortenedString = originalString.substring(0, n) + "..."

It's a bit simple and will cut off words in the middle, but it does the trick. 这有点简单,并且会在中间截断单词,但是可以解决问题。

Use substring method from string java class. 使用字符串java类中的substring方法。 And research better. 并且研究得更好。 I`m sure this question has been answered a lot of times. 我确信这个问题已经回答了很多次。 What do you want is 你想要的是

var string1 = string0.substring(0,n);

since you are using only javascript, here is polyfill taken from https://gist.github.com/timruffles/3377784 由于您仅使用JavaScript,因此这里的polyfill来自https://gist.github.com/timruffles/3377784

 var sample =`It's important to remember that this function does NOT replace newlines with <br> tags. Rather, it inserts a <br> tag before each newline, but it still preserves the newlines themselves! This caused problems for me regarding a function I was writing -- I forgot the newlines were still being preserved.`; var chunk = function(array,chunkSize) { return array.reduce(function(reducer,item,index) { reducer.current.push(item); if(reducer.current.length === chunkSize || index + 1 === array.length) { reducer.chunks.push(reducer.current); reducer.current = []; } return reducer; },{current:[],chunks: []}).chunks }; var data = chunk(sample.split(/[ ]+/), 10); data.forEach(function(line){ console.log(line.join(" ")) }); 

If you don't want to cut words in the middle: 如果您不想在中间砍字:

  function shortener(s, n){ var ret = ""; if(s.charAt(n) !== " "){ var fullWords = s.substring(0, n).split(" ").length - 1; ret = s.split(" ").splice(0, fullWords).join(" "); } else{ ret = s.substring(0, n); } return ret + " ..."; } var s = "It's important to remember that this function does NOT replace newlines with <br> tags. Rather, it inserts a <br> tag before each newline, but it still preserves the newlines themselves! This caused problems for me regarding a function I was writing -- I forgot the newlines were still being preserved. "; console.log(shortener(s, 16)); console.log(shortener(s, 4)); console.log(shortener(s, 80)); 

Output: 输出:

  • It's important ... 这一点很重要 ...
  • It's ... 它的 ...
  • It's important to remember that this function does NOT replace newlines with ... 重要的是要记住,此功能不会将换行符替换为...

Standard JavaScript offers three String methods that allow you to cut your string at position n : 标准JavaScript提供了三种String方法,可让您在n位置处剪切字符串:

Just use one of these methods and add ... at the end of your result. 只需使用这些方法之一,然后在结果的末尾添加...


Demo 1 演示1

 var myString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var n = 5; var output = ''; output += myString.slice(0, n) + '...' + '\\n<br>'; // Option 1 output += myString.substring(0, n) + '...' + '\\n<br>'; // Option 2 output += myString.substr(0, n) + '...'; // Option 3 document.body.innerHTML = output; 

See also this Fiddle . 另请参见此Fiddle


Demo 2 演示2

If you want to do this often, you may want to wrap this in a function : 如果您想经常这样做,则可以将其包装在一个函数中:

 var myString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var output = ''; function shorten(str, n) { if(n > 0) { return str.slice(0, n) + '...'; } return '...'; } output += shorten(myString, 20) + '\\n<br>'; output += shorten(myString, 8) + '\\n<br>'; output += shorten(myString, 0); document.body.innerHTML = output; 

See also this Fiddle . 另请参见此Fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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