How can I find out number of spaces in a string using JavaScript only?
"abc def rr tt" // should return 3
"34 45n v" // should return 2
Split on the spaces and get the length:
var length = "abc def rr tt".split(" ").length - 1;
Or write a nifty prototype function:
String.prototype.getWhitespaceCount = function() {
return this.split(" ").length - 1
}
var x = "abc def rr tt";
var length = x.getWhitespaceCount();