is there an instruction in VB.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there an instruccion in VB.NET that tell me how many times a character is
inside a string

eg. I want to know how many times "*" is in the follow string

4fjru*rufjj*hhh*fffff

tks
ken
 
Create a regular expression and get matches out of the Regex. The number of
matches are the number of times the pattern is found in the string (ie, the
number of * in 4fjru*rufjj*hhh*fffff)


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
You could use a string's Split method that creates an array based on a
character found in a string. You could then count how many items are in the
array:

Dim testString as String = "This is a test"
Dim testArray as Array = testString.Split("i")
Dim answer as integer = testArray.Length

This would return the number 2 to you, which is the number of times the
letter "i" appeared in the original string.

To shorten up what I have above:

Dim answer as Integer = "This is a test".split("i"c).length
 
That works, thk

But anyway I´ll try making a Reg Expresion, I think the performance must be
better with an Reg Exp than using Split because of the array

tnks guys
 
Using Regex would be overkill for this operation and so would using
string.Split. Why not do the search yourself?

private int CountCharactersInString(string inputString, char textChar)
{
int count = 0;
int len = inputString.Length; // it helps with performance to do this
once
for (int i=0; i<len; i++)
{
if (inputString == textChar)
{
count ++;
}
}

return count;
}
ShaneB
 
Pretty interesting

The documentation says that "Regular Expressions Language is designed and
optimized to manipulate text"

Now every option works good, how can I measure each one

ShaneB said:
Using Regex would be overkill for this operation and so would using
string.Split. Why not do the search yourself?

private int CountCharactersInString(string inputString, char textChar)
{
int count = 0;
int len = inputString.Length; // it helps with performance to do this
once
for (int i=0; i<len; i++)
{
if (inputString == textChar)
{
count ++;
}
}

return count;
}
ShaneB

Ken said:
That works, thk

But anyway I´ll try making a Reg Expresion, I think the performance must
be
better with an Reg Exp than using Split because of the array

tnks guys
 
Measure it in terms of how maintainable the code is. If you're talking about performance then unless you have identified this as a perfomance bottleneck, you are attempting premature optimization. People can spend hours, days or even weeks trying to optimize things they *think* will be an issue that turn out to be irrelvant for their application. You only know what is a performance issue (if anything) by measuring.

Regards

Richard Blewett- DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

Pretty interesting

The documentation says that "Regular Expressions Language is designed and
optimized to manipulate text"

Now every option works good, how can I measure each one
 
I agree with you about premature optimization but in this case, but the
creation of another class to do something so simple seems like overkill to
me. If the strings were very large, I might consider using Regex because it
may have better optimizations in it.

Ken, unless you need speed, any way you choose is fine. You can use a
PerformanceCounter to time it if you want to know which is the fastest
method.

ShaneB
 
Ken

You did say you were looking for a VB solution. A quick one liner like this
will do it. Probably not as efficient as many other ways but quick n' easy:-

len("aRbRcR") - len(replace("aRbRcx","R",""))

This single line returns 2! i.e. the R's in the string are replaced by null
and thus reduce the length of the string by the number of substitutions.

Paul Shaddick, MCP
 
Ken,

Not that your solution wouldn't work, but you are using left over string
functions from VB 6.0, rather than the OO string methods that are available
now. In many cases, using the "old" style VB 6.0 functions will not perform
as well as the new string functions of VB.NET & the .NET Framework.
 
Back
Top