Sunday, August 12, 2012

Len and Trim alternatives

From some alternative vb functions, these two are my most recent ones.

Option Explicit

Private Function newlen1(ByVal str As String) As Long
'//Author : s0ft(aayush.babu@yahoo.com)
'//Web    : c0dew0rth.blogspot.com
'//May 27, 9 : 03 PM
If str = "" Then newlen1 = 0: Exit Function
Dim cnt As Long
cnt = 0 '//you may or may not include this line
Dim char As String
Do
    char = Mid$(str, 1, cnt + 1)
    If char <> "" Then
        cnt = cnt + 1
    End If
Loop Until str = char
newlen1 = cnt
End Function

Function AltLen2(ByVal sString As String) As Long
    Dim bArr() As Byte

    If sString = vbNullString Then Exit Function
    bArr = sString

    AltLen2 = ((UBound(bArr) + 1) / 2)
End Function

Function replacespace(ByVal mainchar As String) As String
'//Author : s0ft(aayush.babu@yahoo.com)
'//Web    : c0dew0rth.blogspot.com
'//A simple Replace$(somestring, " ", "") replacement
Dim bt() As Byte
bt = mainchar
Dim cnt As Long
For cnt = 0 To UBound(bt) Step 2 '//step 2 is here for avoiding the null that appears after every character when converted to byte array
    If bt(cnt) <> 32 Then '//32 DEC is ascii code for space
        replacespace = replacespace + Chr$(bt(cnt)) '//only non space characters will be appended to alttrim
    End If
Next cnt
End Function


Function AltTrim(ByVal char As String) As String
'//Author : s0ft(aayush.babu@yahoo.com)
'//Web    : c0dew0rth.blogspot.com
'//A Trim$(somestring) alternative
Dim bt() As Byte
bt = char
Dim cnt As Long
Dim i As Long
Dim Acc As Long
Dim determined As String
Dim addflag As Boolean
For cnt = 0 To UBound(bt) Step 2
    If bt(cnt) <> 32 Or addflag = True Then
        AltTrim = AltTrim + determined + Chr$(bt(cnt))
    End If
    If AltTrim <> "" Then '//if the first non space character has been found then
        Dim cnt_ As Long
        Acc = 0 '//initialise the counter for number of spaces
        For cnt_ = (cnt + 2) To UBound(bt) Step 2 '//start processing from the next character
            If bt(cnt_) = 32 Then '//if next character is space again,
                Acc = Acc + 1 '//acc will have the number of spaces found. it's just a counter
                addflag = False '//till now a non space character isn't detected so keep the addflag false
            Else '//if next character is not a space then
                determined = Space$(Acc) '//reaching here at any point in this loop will mean the no. of spaces as reported by acc are followed by a non-space character
                addflag = True '//tell the above statements to add the spaces
                cnt = cnt + Acc * 2 '//tell that we're going forward only from the new non-space character
                Exit For  '//skip the loop as we've found one non-space character after Acc number of spaces. Goto skiploop will do the same thing
            End If
        Next cnt_
'//skiploop: '//remove the comment if u use Goto skiploop instead of Exit For above
    End If
Next cnt
End Function

Private Sub Form_Load()
Debug.Print AltTrim("   c a   t      ")
End Sub
 

No comments:

Post a Comment