On Sep 04, 2006 5:27 PM, [EMAIL PROTECTED] wrote:
>
> On Sep 03, 2006, at 10:12 UTC, Carlos M wrote:
>
> > I found a solution to remove extras spaces (replace 2 or
> > more spaces by 1) on RS forums that is very fast.
>
> I suspect that the Squeeze function in the StringUtils
> module will be faster in most cases.
> <http://www.verex.com/opensource/>
Joe,
I did test the Squeeze function with several strings and it was always
*much more* slower than the solution I posted (tested on a Win2K
machine with 2005r4).
Here's one result using a string of 29,600 characters (different words
separated by more than one space with no line endings and no tabs):
- Squeeze function took 148 ticks (2.47 secs)
- The solution I posted took 1 tick (0.016 sec)
Even with smaller strings (about 3,000 chars), the Squeeze function is
also slower.
Anyway, I took your Squeeze function and created a new one that is as
faster as the one I posted and added the possibility to use
case-sensitive:
Function MySqueeze(s As String, charSet As String=" ", caseSensitive
As Boolean=False) As String
// Find any repeating characters, where the character is a member of
// charSet, and replace the run with a single character. Examples:
// MySqueeze("wooow maaan", "aeiou") = "wow man"
// MySqueeze("wooooow man", " o") = "wow man"
// MySqueeze("wOOOw maaan", "Oa", True) = "wOw man"
Dim arrChar() As String
Dim char As String
Dim i, numChars, oldLen, newLen As Integer
arrChar = Split(charSet, "")
numChars = UBound(arrChar)
newLen = Len(s)
For i = 0 To numChars
char = arrChar(i)
Do
oldLen = newLen
If caseSensitive Then
s = ReplaceAllB(s, char + char, char)
Else
s = ReplaceAll(s, char + char, char)
End if
newLen = Len(s)
Loop Until newLen = oldLen
Next
Return s
End Function
Tested this MySqueeze function with a string with 39,441 chars (words
separated by one space) where *all* vowels were repeated 3 times and
the time it took to "squeeze" it was just 3 ticks (0.031 secs). With
the StringUtils.Squeeze it took 267 ticks (4.468 secs).
If you find this useful for the StringUtils feel free to use it or
adapt it.
Carlos
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>