[英]Unable to Find string length without white space using javascript
我的示例代码是:
<script>
function Sum(){
var a = "Hello World";
var a_length =a.length ;
alert(a_length);
}
</script>
当前长度为:: 11。 但是我想计算将为10的字符串长度。
请帮助使用JavaScript获取正确的长度。
这将100%起作用。
function Sum(){
var myString = "Hello World";
var withoutSpace = myString.replace(/ /g,"");
var length = withoutSpace.length;
alert(length);
}
**For length including white-space:**
$("#id").val().length
**For length without white-space:**
$("#id").val().replace(/ /g,'').length
**For removing only beginning and trailing white-space:**
$.trim($("#test").val()).length
**For example, the string " t e s t " would evaluate as:**
*//" t e s t "*
$("#id").val();
**//Example 1**
$("#id").val().length; //Returns 9
**//Example 2**
$("#id").val().replace(/ /g,'').length; //Returns 4
**//Example 3**
$.trim($("#test").val()).length; //Returns 7
希望它对您有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.