Reputation: 164
My sample code is:
<script>
function Sum(){
var a = "Hello World";
var a_length =a.length ;
alert(a_length);
}
</script>
The current length is ::11 . But i want to count the string length that will be 10.
Please help to get proper length using javascript.
Upvotes: 1
Views: 100
Reputation: 1176
This will work 100 %.
function Sum(){
var myString = "Hello World";
var withoutSpace = myString.replace(/ /g,"");
var length = withoutSpace.length;
alert(length);
}
Upvotes: 3
Reputation: 505
**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
Try hope it's works for you
Upvotes: 2