0

コードは次のようになります。

 function TrimLength(text, maxLength) {
        text = $.trim(text);

        if (text.length > maxLength) {
            text = text.substring(0, maxLength - ellipsis.length)
            return text.substring(0, text.lastIndexOf(" ")) + ellipsis;
        }
        else
            return text;
    }

私が抱えている問題は、次のことを行うことです。

hello world and an...

The curse of the gaming backlog –...

代わりに次のことを確認したい:

hello world and...

The curse of the gaming backlog...

(a、b、c、dなど)のようなアルファベット文字があり、特殊文字がないことを確認する必要があると思います。

どんな種類の助けも大歓迎です

4

1 に答える 1

0

これから始めたいと思うかもしれません:

function cutoff(str, maxLen) {
    // no need to cut off
    if(str.length <= maxLen) {
        return str;
    }

    // find the cutoff point
    var oldPos = pos = 0;
    while(pos!==-1 && pos <= maxLen) {
        oldPos = pos;
        pos = str.indexOf(" ",pos) + 1;
    }
    if (pos>maxLen) { pos = oldPos; }
    // return cut off string with ellipsis
    return str.substring(0,pos) + "...";
}

少なくとも、文字ではなく単語に基づいてカットオフを提供します。追加のフィルタリングが必要な場合は、それを追加できますが、これにより、「ゲームのバックログの呪い – ...」などのカットオフが得られます。

于 2013-06-02T02:03:57.877 に答える