Stephany Young wrote:
Take his word for it that it is certainly resource effective and scalable.
Whether it is the most ... is a matter of debate.
For short strings it will be lightening fast.
Yes, but that is not very interresting. For short strings it doesn't
really matter. It's what happens when the strings get larger that
matters for scalability.
For longer strings, the further it has to loop through the characters
the longer it will take and you might find it quicker to do something like:
Function IsAllSameCharacter(ByVal s As String)
Dim _s As String = New String(s.Chars(0), s.Length)
Return (s = _s)
End Function
But that will make two loops instead of one. First it will do a loop to
initialise the new string, then it will do another loop to compare all
the characters between the strings. At best (when all characters are the
same) it's half as fast, at worst (when the first two characters differ)
it's much slower, as the first loop will always loop through all the
characters regardless if they are used or not.
As this method creates another string that is as large as the original
string, it does not scale well. The method that I presented only
allocates memory for a repeater, and that is the same regardless of the
size of the string.
--
Göran Andersson
_____
http://www.guffa.com