Jump to content

counting commas


niche

Recommended Posts

Is there a easiy way to count the number of commas in a comma delimited text string using javascript?If i turned the test string into an array, what would be the easiest way to count elements using javascript?Edit: I found length() in the JavaScript Array Object - Should have node that, but i still could use an answer to my first question.

Link to comment
Share on other sites

Loop throught all the characters of the string using charAt() and add 1 to a counter if a comma is found.Here's an example of a character-finding method for the String object:

String.prototype.countOccurences = function(char) {  amount = 0;  for(var i = 0; i < this.length; i++) {	if(this.charAt(i) == char) {	  amount++;	}  }  return amount;}var str = "A string, with a certain amount of commas, which are to be counted.";alert( str.countOccurences(",") );

Link to comment
Share on other sites

Hey Ingolme,Lemme run this by you to make sure I understand what you did:You added a method to the String Object using the prototype called CountOccureneces and it accepts one parameter.It is called in the alert by str.countOccurences(",") where what's being passed in is ',' comma.str is the string we're counting for the commas.The 'this' in the function is referring(referencing) to str(the string). We loop through the string(str) and if we find a comma(by using CharAt() == char), char being the comma passed into the function then we add 1 to the amount variable. When it's done looping through, then return amount which is then displayed in the alert where the function was called.Is my interpretation correct? Thanks!

Loop throught all the characters of the string using charAt() and add 1 to a counter if a comma is found.Here's an example of a character-finding method for the String object:
String.prototype.countOccurences = function(char) {  amount = 0;  for(var i = 0; i < this.length; i++) {	if(this.charAt(i) == char) {	  amount++;	}  }  return amount;}var str = "A string, with a certain amount of commas, which are to be counted.";alert( str.countOccurences(",") );

Link to comment
Share on other sites

I would only recommend that you put the var keywork before you define amount, as without it, javascript will turn into a global variable, and amount is a very common variable name.

Link to comment
Share on other sites

Hey Ingolme,Lemme run this by you to make sure I understand what you did:You added a method to the String Object using the prototype called CountOccureneces and it accepts one parameter.It is called in the alert by str.countOccurences(",") where what's being passed in is ',' comma.str is the string we're counting for the commas.The 'this' in the function is referring(referencing) to str(the string). We loop through the string(str) and if we find a comma(by using CharAt() == char), char being the comma passed into the function then we add 1 to the amount variable. When it's done looping through, then return amount which is then displayed in the alert where the function was called.Is my interpretation correct? Thanks!
Yeah, that's all correct.
I would only recommend that you put the var keywork before you define amount, as without it, javascript will turn into a global variable, and amount is a very common variable name.
Yes, I wrote it on the spot and missed out the var statement. I agree with you, declaring variables is very important.
Link to comment
Share on other sites

Thanks Deirdre's Dad and Ingolme for your help and Don E for your participation. It's always good to know that there are others with the same question.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...