How to count the number of characters without spaces?

前端 未结 3 1119
渐次进展
渐次进展 2020-12-06 14:23

I\'m new to this, so please understand me;/

I\'m creating an app in appery.io and it has to count the number of letters of text inserted by the app user(without spac

相关标签:
3条回答
  • 2020-12-06 14:35

    To ignore a literal space, you can use regex with a space:

    // get the string
    let myString = getElementById("input").value;
    
    // use / /g to remove all spaces from the string
    let remText = myString.replace(/ /g, "");
    
    // get the length of the string after removal
    let length = remText.length;
    

    To ignore all white space(new lines, spaces, tabs) use the \s quantifier:

    // get the string
    let myString = getElementById("input").value;
    
    // use the \s quantifier to remove all white space
    let remText = myString.replace(/\s/g, "")
    
    // get the length of the string after removal
    let length = remText.length;
    
    0 讨论(0)
  • 2020-12-06 14:45

    You can count white spaces and subtract it from lenght of string for example

    var my_string = "John Doe's iPhone6";
    var spaceCount = (my_string.split(" ").length - 1);
    console.log(spaceCount);
    console.log('total count:- ', my_string.length - spaceCount)
    
    0 讨论(0)
  • 2020-12-06 14:51

    Use this:

    var myString = getElementById("input").value;
    var withoutSpace = myString.replace(/ /g,"");
    var length = withoutSpace.length;
    
    0 讨论(0)
提交回复
热议问题