Display only 10 characters of a long string?

前端 未结 12 2083
臣服心动
臣服心动 2020-12-13 01:19

How do I get a long text string (like a querystring) to display a maximum of 10 characters, using JQuery?

Sorry guys I\'m a novice at JavaScript & JQuery :S

相关标签:
12条回答
  • 2020-12-13 01:49

    Creating own answer, as nobody has considered that the split might not happened (shorter text). In that case we don't want to add '...' as suffix.

    Ternary operator will sort that out:

    var text = "blahalhahkanhklanlkanhlanlanhak";
    var count = 35;
    
    var result = text.slice(0, count) + (text.length > count ? "..." : "");
    

    Can be closed to function:

    function fn(text, count){
        return text.slice(0, count) + (text.length > count ? "..." : "");
    }
    
    console.log(fn("aognaglkanglnagln", 10));
    

    And expand to helpers class so You can even choose if You want the dots or not:

    function fn(text, count, insertDots){
        return text.slice(0, count) + (((text.length > count) && insertDots) ? "..." : "");
    }
    
    console.log(fn("aognaglkanglnagln", 10, true));
    console.log(fn("aognaglkanglnagln", 10, false));
    
    0 讨论(0)
  • 2020-12-13 01:50
    var example = "I am too long string";
    var result;
    
    // Slice is JS function
    result = example.slice(0, 10)+'...'; //if you need dots after the string you can add
    

    Result variable contains "I am too l..."

    0 讨论(0)
  • 2020-12-13 01:52

    This looks more to me like what you probably want.

    $(document).ready(function(){
    var stringWithShorterURLs = getReplacementString($(".tasks-overflow").text());
    
    function getReplacementString(str){
        return str.replace(/(https?\:\/\/[^\s]*)/gi,function(match){
            return match.substring(0,10) + "..."
        });
    }});
    


    you give it your html element in the first line and then it takes the whole text, replaces urls with 10 character long versions and returns it to you. This seems a little strange to only have 3 of the url characters so I would recommend this if possible.

    $(document).ready(function(){
    var stringWithShorterURLs = getReplacementString($(".tasks-overflow p").text());
    
    function getReplacementString(str){
        return str.replace(/https?\:\/\/([^\s]*)/gi,function(match){
            return match.substring(0,10) + "..."
        });
    }});
    

    which would rip out the http:// or https:// and print up to 10 charaters of www.example.com

    0 讨论(0)
  • 2020-12-13 01:53

    Show this "long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text long text "

    to

    long text long text long ...

            function cutString(text){    
                var wordsToCut = 5;
                var wordsArray = text.split(" ");
                if(wordsArray.length>wordsToCut){
                    var strShort = "";
                    for(i = 0; i < wordsToCut; i++){
                        strShort += wordsArray[i] + " ";
                    }   
                    return strShort+"...";
                }else{
                    return text;
                }
             };
    
    0 讨论(0)
  • 2020-12-13 01:54

    Try this :)

    var mystring = "How do I get a long text string";
    mystring = mystring.substring(0,10);
    alert(mystring);
    
    0 讨论(0)
  • 2020-12-13 01:58

    If I understand correctly you want to limit a string to 10 characters?

    var str = 'Some very long string';
    if(str.length > 10) str = str.substring(0,10);
    

    Something like that?

    0 讨论(0)
提交回复
热议问题