Javascript 如何在段落中的第一行之后切断字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8436509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:00:45  来源:igfitidea点击:

How to cut off string after the first line in the paragraph

javascriptstringcut

提问by Zhen

I have the string as show below:

我有如下所示的字符串:

XXX:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur cursus lacus sed
justo faucibus id pellentesque nunc porttitor. Sed venenatis tempor dui, nec mattis dolor
ultrices at. Duis suscipit, dolor sed fringilla interdum, magna libero tempor quam, sed
molestie dui urna sed tellus.

How can I add a restriction and cut the string off at the first line? (using javascript).

如何添加限制并在第一行切断字符串?(使用javascript)。

The end result I would expect is as follows:

我期望的最终结果如下:

XXX:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur...

回答by Tomalak

var firstLine = theString.split('\n')[0];

回答by Wilt

Use the optional limit param for increased performance

使用可选的限制参数来提高性能

Tomalak his answer is correct, but in case you want to really only match the first lineit will be useful to pass the optional second limitparameter. Like this you prevent that a long string (with thousands of lines) will be splitted till the end before the first match is returned.

Tomalak 他的回答是正确的,但如果你真的只想匹配第一行,传递可选的第二个limit参数会很有用。像这样,您可以防止在返回第一个匹配项之前将长字符串(包含数千行)拆分到最后。

With setting the optional limitto 1we tell the method to return the result as soon as the first match is found with increased performance as a result.

通过设置可选项limit1我们告诉方法在找到第一个匹配项后立即返回结果,从而提高性能。

var firstLine = theString.split('\n', 1)[0];

Read more on the limit param for example here in the MDN docs

阅读更多关于限制参数的信息,例如在 MDN 文档中

回答by Alex Turpin

If there are actual line returns, and not just some kind of auto-wrapping, you can do this:

如果有实际的行返回,而不仅仅是某种自动换行,您可以这样做:

str = str.substr(0, str.indexOf("\n"));

http://jsfiddle.net/f6uBT/

http://jsfiddle.net/f6uBT/

回答by ujeenator

function getFirstLine(str){
    var breakIndex = str.indexOf("\n");

   // consider that there can be line without a break
    if (breakIndex === -1){
        return str;
    }

    return str.substr(0, breakIndex);
}

getFirstLine('first line\nsecond line'); // first line

getFirstLine('text without line break'); // text without line break

回答by Oded BD

you should use this function

你应该使用这个功能

string.split(separator, [limit])

string.split(分隔符,[限制])

separator - the char to split [". " or \r\n ...] limit - optional, int to limit the max chars

分隔符 - 要拆分的字符 [". " 或 \r\n ...] 限制 - 可选,用于限制最大字符数的 int

回答by Nick Perkins

Interesting discussion about "wrapped text"... Maybe it's more of an HTML problem than a javascript problem...

关于“换行文本”的有趣讨论......也许它更像是一个 HTML 问题而不是一个 javascript 问题......

Maybe what you really want is to limit the height of the HTML element, and set "overflow=hidden". Then it will display as much as it can fit in one line, and hide the rest. ( but you will not get the little "..." at the end )

也许你真正想要的是限制 HTML 元素的高度,并设置“overflow=hidden”。然后它将尽可能多地显示在一行中,并隐藏其余部分。(但你不会得到最后的小“...”)

回答by asawyer

var str = document.getElementsByTagName("div")[0].innerHTML;

var firstLine = function(input,cutlength,appendtext){
    if(input.length<=cutlength)
        return input;
    return input.substr(0,cutlength) + appendtext;
}


alert(firstLine(str,50,"..."));

edit - here's a fiddle link http://jsfiddle.net/a3C86/

编辑 - 这是一个小提琴链接http://jsfiddle.net/a3C86/