Thread: Function strSqueezeConsecutive(strIn As String, i ( Function strSqueezeCo

1. Function strSqueezeConsecutive(strIn As String, i ( Function strSqueezeCo

<pre>Public Function strSqueezeConsecutive(strIn As String, iCount As Integer) As String
' Procedure : strSqueezeConsecutive
' Description: Return the supplied string with not more than iCount
' consecutive repetions of any character.
' Inputs: STRING, Integer
' Returns: STRING
' Assumes: Nothing
' Side Effects: None.
' Tested: By the calls shown below.

Dim strResult As String
strResult = ""
Dim iSame As Integer
iSame = 1
Dim strChPrevious As String
strChPrevious = ""
Dim ich As Integer
For ich = 1 To Len(strIn)
If strChPrevious = Mid\$(strIn, ich, 1) Then
iSame = iSame + 1
If iSame > iCount Then
Else
strResult = strResult & Mid\$(strIn, ich, 1)
End If
Else
strChPrevious = Mid\$(strIn, ich, 1)
iSame = 1
strResult = strResult & Mid\$(strIn, ich, 1)
End If
Next ich
strSqueezeConsecutive = strResult
'Sub TESTstrSqueezeConsecutive()
'MsgBox strSqueezeConsecutive("abbcccddddded", 3) ' abbcccddded
'MsgBox strSqueezeConsecutive("abbcccddddded", 2) ' abbccdded
'MsgBox strSqueezeConsecutive("abbcccddddded", 1) ' abcded
'MsgBox strSqueezeConsecutive("abbcccddddded", 0) ' abcded
'End Sub
End Function
</pre>

2. Re: Function strSqueezeConsecutive(strIn As String, i ( Function strSquee

Purpose? I can see what it does, but I have trouble figuring out why you would need it.

3. Re: Function strSqueezeConsecutive(strIn As String, i ( Function strSquee

If I can throw in my 2 cents worth - I use a very similar "squeeze" function quite often to get rid of white space, especially as part of removing blank paragraphs at the end of a document. Yes, I use "trim" as well, but a generic "squeeze" can be very useful for getting rid of stuff in the middle of other stuff.

Cheers,
Margaret

4. Re: Function strSqueezeConsecutive(strIn As String, i ( Function strSquee

Edited by Charlotte to eliminate horizontal scrolling

> similar "squeeze" function quite often to get rid of white space,

Quite so. I have strReplaceAll and similar functions, also a table-driven set of rules for cleaning a document.

The interesting part of this (posted) function is the short time it took to sketch, code and test a wrapper function, which would be applicable to me if I were sitting at a client desk and couldn't access my library or Woody's Lounge etc.

I've rarely timed myself for the complete delivery of a general-purpose function.

This (posted) one, by the way, isn't geared to any specific characters, such as "White Space". It would be a simple matter to include an Optional parameter that caused squeezing ONLY of those nominated characters, then your use to squeeze out multiple white-space would be:

<pre>strUser = strSqueezeConsecutive(strUser,1,chr\$(032) & _
chr\$(160) & chr\$(007) \$ chr\$(009))
</pre>

or even:
<pre>strUser = strSqueezeConsecutive(strUser,1,strWhiteSpace)
</pre>

5. Re: Function strSqueezeConsecutive(strIn As String, i ( Function strSquee

Oh, I use the Replace() function to get rid of white space on occasion. I just didn't see any purpose in stripping out a single instance of a character in a string. The only use I could think of would be in building a soundex value, and that's usually included in the algorithm, since it examines each character.

6. Re: Function strSqueezeConsecutive(strIn As String, i ( Function strSquee

>Purpose? I can see what it does, but I have trouble figuring out why you would need it.

Well, funny you should ask but ....

A Software House needed to code and test a wrapper to eliminate repeated consecutive characters from a username or password string. They'd use it like this:

<pre> Dim StrUser as string
strUser = ... mechanism to obtain user's name, or password etc.
if strUser = strSqueezeConsecutive(strUser,3) Then
else
MsgBox "Too many consecutive characters in " & strUser
endif
</pre>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•