Quantify items in a string

G

Guest

OK, I have a string that reads like this:

[Group1][Group3][Group3]...[Group10]

How do I quantify how many [*] occurences there are in classic ASP? Do
i use the inStr function and if so, how???

Here's my thought process...

if groups > 1 then
do this
else 'more than 1 group present
do that
end if

But how do I loop through the string and quantify how many groups
there are using the above string?

Thanks in advance!
 
A

Aaron Bertrand - MVP

If a group is signified by ending in a "]" then you can do this:

groupString = "[Group1][Group2][Group3]"
Response.Write len(GroupString) - len(replace(GroupString, "]", "")) & "
groups."

You could also do this:

groupString = "[Group1][Group2][Group3]"
Response.Write (ubound(split(groupString, "][")) + 1) & " groups."
 
D

Dan Brussee

OK, I have a string that reads like this:

[Group1][Group3][Group3]...[Group10]

How do I quantify how many [*] occurences there are in classic ASP? Do
i use the inStr function and if so, how???

Here's my thought process...

if groups > 1 then
do this
else 'more than 1 group present
do that
end if

But how do I loop through the string and quantify how many groups
there are using the above string?

Thanks in advance!

If your spec is exactly as shown, I would say to use the split
operator using the string "][" as a seperator.

dim a, groupcount
a = split("[Group1][Group3][Group9]", "][")
groupcount = ubound(a) + 1

would give you an array of 3 items.

a(0) = "[Group1"
a(1) = "Group3"
a(2) = "Group9]"

groupcount would equal 3.
 
C

Chris Hohmann

Sans Spam said:
OK, I have a string that reads like this:

[Group1][Group3][Group3]...[Group10]

How do I quantify how many [*] occurences there are in classic ASP? Do
i use the inStr function and if so, how???

Here's my thought process...

if groups > 1 then
do this
else 'more than 1 group present
do that
end if

But how do I loop through the string and quantify how many groups
there are using the above string?

Thanks in advance!

<script language="JavaScript" runat="SERVER">
Response.Write("[Group1][Group2][Group3]".match(/\[.*?\]/g).length);
</script>

-Chris Hohmann
 
D

Dan Brussee

Sans Spam said:
OK, I have a string that reads like this:

[Group1][Group3][Group3]...[Group10]

How do I quantify how many [*] occurences there are in classic ASP? Do
i use the inStr function and if so, how???

Here's my thought process...

if groups > 1 then
do this
else 'more than 1 group present
do that
end if

But how do I loop through the string and quantify how many groups
there are using the above string?

Thanks in advance!

<script language="JavaScript" runat="SERVER">
Response.Write("[Group1][Group2][Group3]".match(/\[.*?\]/g).length);
</script>

I knew I should have looked at the regular expression method :)
 
C

Chris Hohmann

Dan Brussee said:
Sans Spam said:
OK, I have a string that reads like this:

[Group1][Group3][Group3]...[Group10]

How do I quantify how many [*] occurences there are in classic ASP? Do
i use the inStr function and if so, how???

Here's my thought process...

if groups > 1 then
do this
else 'more than 1 group present
do that
end if

But how do I loop through the string and quantify how many groups
there are using the above string?

Thanks in advance!

<script language="JavaScript" runat="SERVER">
Response.Write("[Group1][Group2][Group3]".match(/\[.*?\]/g).length);
</script>

I knew I should have looked at the regular expression method :)

Yeah, RE's are pretty sexy. Although, for small input strings like this,
the Replace function is actually faster. With RegExp, there's the object
implementation to contend with. However, the RegExp object scales much
better, so for large input (i.e. files), RE's are the way to go. RE's
are also much more powerful. Try validating email addresses with
Left/Right/InStr/Len, then try doing it with RE's. You'll never look
back. Give me an regular expression engine and a looping construct and
I'll rule the world.

-Chris Hohmann
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top