How to do text truncation without cutting .Net words?

2

I'm trying in a number of ways to days but I have not been able to get this script to get the value of <h3> and return the text to it truncated.

JavaScript:

<script type="text/javascript">
    function truncar() {

        var texto = document.getElementById("texto_noticia");
        var limite = 350;

        if (texto.length > limite) {
            limite--;
            last = texto.substr(limite - 1, 1);
            while (last != ' ' && limite > 0) {
                limite--;
                last = texto.substr(limite - 1, 1);
            }
            last = texto.substr(limite - 2, 1);
            if (last == ',' || last == ';' || last == ':') {
                texto = texto.substr(0, limite - 2) + ' [...]';
            } else if (last == '.' || last == '?' || last == '!') {
                texto = texto.substr(0, limite - 1);
            } else {
                texto = texto.substr(0, limite - 1) + ' [...]';
            }
        }

        alert(texto);
        return (texto);
    }
</script>

HTML:

<h3 id="texto_noticia" class="texto_noticia">
        <%# Eval("Noticia_Conteudo")%>
</h3>

The idea is to bring news content that is in the database through repeater , and then have JavaScript take the value and truncate the text.

    
asked by anonymous 27.09.2016 / 14:31

1 answer

3

The key is to use LastindexOf(' ') to find out where is the last space. It would be something like this in C #:

if (text.Length <= length) {
    return text;
}
if (length <= ellipsis.Length) {
    return ellipsis.Substring(0, length);
}
text = text.Substring(0, length - ellipsis.Length);
var position = text.LastIndexOf(' ');
position = position < 0 ? 0 : position;
return text.Substring(0, position).Trim(new[] { '.', ',', ';', ':', '!', '?' }) + ellipsis;

See running on dotNetFiddle and CodingGround .

If you want to do in JS is a simple algorithm conversion. JS has all methods used with identical semantics.

function trimSpecials(text, chars) {
    var result = '';
    for(var i = 0; i < text.length; i++) {
        var isNormal = true;
        for(var j = 0; j < text.length; j++) {
            if (text[i] == chars[j]) {
                isNormal = false;
                break;
            }
        }
        if (isNormal) {
            result += text[i];
        }
    }
    return result;
}

function ellipsis(text, length, ellipsis) {
    ellipsis = typeof ellipsis !== 'undefined' ? ellipsis : '...';
    if (text.length <= length) {
        return text;
    }
    if (length <= ellipsis.length) {
        return ellipsis.substr(0, length);
    }
    text = text.substr(0, length - ellipsis.length);
    var position = text.lastIndexOf(' ');
    position = position < 0 ? 0 : position;
    return trimSpecials(text.substr(0, position), ['.', ',', ';', ':', '!', '?'])  + ellipsis;
}

var texto = "Este é um teste de texto longo que precisa ser truncado com reticências, sem cortar a palavra.";
for (var i = 0; i < texto.length + 1; ++i) {
    document.body.innerHTML += i + ' : ' + ellipsis(texto, i) + '<br>';
}
    
27.09.2016 / 15:07