January 1, 2005

WinTask - CounterString


'
' CounterString.src
'
' Generate a "Counter String" similar to James Bach's example
' (see: http://www.satisfice.com/blog/archives/22)
'
' Author: Joe Strazzere
'
' CounterString$({num},{char})
'
' Produces a special string of length {num} that counts its own characters.
'
' For example, CounterString$(10, "*") would produce "*3*5*7*10*"
' which is a ten-character long string, such that each asterisk is at a
' position in the string equal to the number that precedes it.
'
' This is useful for pasting into fields that cut off text,
' so that you can tell how many characters were actually pasted.
'
' You can specify a separator other than asterisk. CounterString$(15, "A")
' would produce "A3A5A7A9A12A15A"


Function CounterString$(TargetLen, Marker$)
Local MyCounterString$
MyCounterString$ = Str$(TargetLen) + Marker$

If TargetLen < 1 Then CounterString$="ERROR - TargetLen must be > 0. Counter$(TargetLen, Marker$)"
ExitFunction
EndIf

If Len(Marker$) <> 1 Then
CounterString$="ERROR - Marker$ must be a single character. Counter$(TargetLen, Marker$)"
ExitFunction
EndIf

While Len(MyCounterString$) < TargetLen If Left$(MyCounterString$,1) <> Marker$ Then
MyCounterString$ = Marker$ + MyCounterString$
Else
MyCounterString$ = Str$(TargetLen - Len(MyCounterString$) + 1) + MyCounterString$
EndIf
Wend

CounterString$ = MyCounterString$

EndFunction

'Examples
MsgBox(CounterString$(5,"*"))
MsgBox(CounterString$(23,"*"))
MsgBox(CounterString$(17,"_"))
MsgBox(CounterString$(103,"."))