I have a string "John Doe's iPhone6"
Visually, I know that it contains 2 spaces.
How do I count spaces in a string in javascript ?
I've tried
var input = this.value;
// console.log(input.count(' '));
I have a string "John Doe's iPhone6"
Visually, I know that it contains 2 spaces.
How do I count spaces in a string in javascript ?
var input = this.value;
// console.log(input.count(' '));
Try this
var my_string = "John Doe's iPhone6";
var spaceCount = (my_string.split(" ").length - 1);
console.log(spaceCount)
Use RegExp:
"John Doe's iPhone6".match(/([\s]+)/g).length
Use split and count them less 1 (-1):
var string = "John Doe's iPhone6";
string.split(" ").length-1