Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to return value the finds all instances of value

Status
Not open for further replies.

ryan

Programmer
Nov 13, 2000
73
US
I'm trying to find out the command that will allow me to do this:

Say I have a string called and it contains "1,2,3,4,5" ... I want to know what is the command that counts all the instances of the character "," in that string. If there is such a command it would return 4 since there are 4 "," in that string.

Thanks,
Ryan
 
Not sure that there is a specific function,
but if you use the following (not tested) you will get the answer.

Dim strString, intCount, charCount
strString = "1,2,3,4,5,6"
charCount=0

for intCount=1 to len(strString)
if mid(strString,intCount,1) ="," then
charCount=charCount+1
end if
next
response.write "character count = " & charCount


Steve Davis
hey.you@hahaha.com.au
 
Well if you 'split' the string into an Array using the character of interest as a delimiter then the length of the array minus one would be the count of characters, or am I missing something?

// javascript
var mystr = "1,2,3,4,5";
var anArray = mystr.split(",");
var countOfCommas = anArray.length -1;

-pete
 
Wow ... good catch Pete, that does work. Thanks guys.

Ryan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top