Javascript Count Character Occurrence

How To Count Specific Character Occurrence In A String Using Javascript 

Number Of Specific Char In A String Using Javascript


In This Javascript Tutorial we will See How To Count The Number Of Selected Char Occurrences In A Text Using JS And Netbeans Editor .


Project Source Code:

    
<!DOCTYPE html>
<html>
    <head>
        <title>Javascript Tutorial</title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <p>Put Your Text Here To Search On It</p>

        <script>
            
            var txt = "Put Your Text Here To Search On It",
                count = 0;
            
            for(var i = 0; i < txt.length; i++){
                
                if(txt.charAt(i) === 'r'){
                    count++;
                }
                
            }
            console.log(count);
            
        </script>
     
    </body>
</html>




OUTPUT: 3







Share this

Related Posts

Previous
Next Post »