Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count Space(s) in a string [duplicate]

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(' '));
like image 517
code-8 Avatar asked Mar 07 '16 16:03

code-8


1 Answers

Try this

var my_string = "John Doe's iPhone6";
var spaceCount = (my_string.split(" ").length - 1);
console.log(spaceCount)
like image 148
Mayki Nayki Avatar answered Sep 18 '22 18:09

Mayki Nayki