Browse By

How to Split String, count element and implement loop in Jquery?

Spread the love

Split Sting with function :

string.split(',');

Count element:

stringArr.length;

Complete code is give below.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
 	var string = 'Harry,John,Clark,Peter,Rohn,Alice';
	var nameArr = string.split(',');
	
	for(var i = 0; i < nameArr.length; i++)
	{                    
		alert(nameArr[i]);
    }
  
});
</script>

explain code :

string.split(‘,’) is used for spliting string into arrray which can be achieve by using single value like alert(nameArr[1])

nameArr.length is userd for counting Array length

for(var i = 0; i < nameArr.length; i++) is used for looking the variable till end of the length of string.

Output:

pop

Leave a Reply

Your email address will not be published. Required fields are marked *