Count the number of occurrences of a character in a string in Javascript

后端 未结 30 2914
礼貌的吻别
礼貌的吻别 2020-11-22 02:33

I need to count the number of occurrences of a character in a string.

For example, suppose my string contains:

var mainStr = \"str1,str2,str3,str4\";         


        
30条回答
  •  日久生厌
    2020-11-22 03:12

    What about string.split(desiredCharecter).length-1

    Example:

    var str = "hellow how is life"; var len = str.split("h").length-1; will give count 2 for character "h" in the above string;

提交回复
热议问题