【问题标题】:Add dots at a position in text without truncating words [duplicate]在文本中的某个位置添加点而不截断单词[重复]
【发布时间】:2017-07-07 02:17:57
【问题描述】:

如何在文本中的某个位置添加三个点,防止这三个点截断任何单词或放在标点符号(.,;:)之后?

这是我的javascript函数

let parseText = function(text, limit){
    return text.substring(0, limit) + '...';
};

【问题讨论】:

  • 我也在寻找防止在标点符号后放置三个点

标签: javascript string truncate


【解决方案1】:

我的answerthis 的帖子将对您有所帮助:

用于防止单词中间或标点符号后面的点。

let parseText = function(text, limit){
if (text.length > limit)
    for (let i = limit; i > 0; i--){
        if(text.charAt(i) === ' ' && (text.charAt(i-1) != ','||text.charAt(i-1) != '.'||text.charAt(i-1) != ';')) {
            return text.substring(0, i) + '...';
        }
    }
else
    return text;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2016-05-27
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    相关资源
    最近更新 更多