<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SnippetWare.com &#187; Strings</title>
	<atom:link href="http://www.snippetware.com/category/vb/strings/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.snippetware.com</link>
	<description>Code snippets library</description>
	<lastBuildDate>Fri, 22 Jan 2010 12:04:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Fast String Manipulation</title>
		<link>http://www.snippetware.com/2009/10/11/fast-string-manipulation/</link>
		<comments>http://www.snippetware.com/2009/10/11/fast-string-manipulation/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 13:10:49 +0000</pubDate>
		<dc:creator>Zaur</dc:creator>
				<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.snippetware.com/?p=284</guid>
		<description><![CDATA[<p><a href="http://www.snippetware.com/2009/10/11/fast-string-manipulation/">Continue</a></p>]]></description>
			<content:encoded><![CDATA[<pre class="brush: vb">
Option Explicit

Private lPosition As Long
Private strBigString As String

Private Declare Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Public Sub AddToString(strAdd As String)
    &#039; If the current position + length of string to add is greater
    &#039; than the length of the BigString, then increase the size of
    &#039; the BigString
    If lPosition + LenB(strAdd) &gt; LenB(strBigString) Then strBigString = strBigString &amp; Space$(10000)

    &#039; Add strAdd to the BigString
    CopyMemory ByVal StrPtr(strBigString) + lPosition, ByVal StrPtr(strAdd), LenB(strAdd)

    &#039; Move the pointer to show where the end of the string is
    lPosition = lPosition + LenB(strAdd)
End Sub

Public Property Get ReturnString() As String
    &#039; Trim off the blank characters and return the string
    strBigString = left$(strBigString, lPosition \ 2)      &#039;trim back to size
    ReturnString = strBigString
End Property

Private Sub Class_Initialize()
    &#039;Set the starting position of the BigString to 0
    lPosition = 0
End Sub

Public Sub Initialise()
    &#039; Re-Initialise String
    lPosition = 0
    strBigString = vbNullString
End Sub

Public Sub ReplaceEx(sFind As String, sReplace As String, bCompare As VbCompareMethod)
    strBigString = Replace$(strBigString, sFind, sReplace, , , bCompare)
End Sub

Public Sub AddNewString(strAdd As String)
    &#039; Re-Initialise String
    lPosition = 0
    strBigString = vbNullString

    AddToString strAdd
End Sub

Public Sub AddToStringEx( _
        Optional ByVal strAdd1 As String, _
        Optional ByVal strAdd2 As String, _
        Optional ByVal strAdd3 As String, _
        Optional ByVal strAdd4 As String, _
        Optional ByVal strAdd5 As String _
    )

    If Len(strAdd1) &lt;&gt; 0 Then AddToString strAdd1
    If Len(strAdd2) &lt;&gt; 0 Then AddToString strAdd2 Else Exit Sub
    If Len(strAdd3) &lt;&gt; 0 Then AddToString strAdd3 Else Exit Sub
    If Len(strAdd4) &lt;&gt; 0 Then AddToString strAdd4 Else Exit Sub
    If Len(strAdd5) &lt;&gt; 0 Then AddToString strAdd5 Else Exit Sub
End Sub
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.snippetware.com/2009/10/11/fast-string-manipulation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Returns a string without any zero terminator. Typically, this was a string returned by a Windows API call</title>
		<link>http://www.snippetware.com/2009/10/11/returns-a-string-without-any-zero-terminator-typically-this-was-a-string-returned-by-a-windows-api-call/</link>
		<comments>http://www.snippetware.com/2009/10/11/returns-a-string-without-any-zero-terminator-typically-this-was-a-string-returned-by-a-windows-api-call/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 13:10:21 +0000</pubDate>
		<dc:creator>Zaur</dc:creator>
				<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.snippetware.com/?p=282</guid>
		<description><![CDATA[<p><a href="http://www.snippetware.com/2009/10/11/returns-a-string-without-any-zero-terminator-typically-this-was-a-string-returned-by-a-windows-api-call/">Continue</a></p>]]></description>
			<content:encoded><![CDATA[<pre class="brush: vb">
&#039;-----------------------------------------------------------
&#039; FUNCTION: StripTerminator
&#039;
&#039; Returns a string without any zero terminator.  Typically,
&#039; this was a string returned by a Windows API call.
&#039;
&#039; IN: [strString] - String to remove terminator from
&#039;
&#039; Returns: The value of the string passed in minus any
&#039;          terminating zero.
&#039;-----------------------------------------------------------
&#039;
Function StripTerminator(ByVal strString As String) As String
    Dim intZeroPos As Integer

    intZeroPos = InStr(strString, Chr$(0))
    If intZeroPos &gt; 0 Then
        StripTerminator = Left$(strString, intZeroPos - 1)
    Else
        StripTerminator = strString
    End If
End Function
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.snippetware.com/2009/10/11/returns-a-string-without-any-zero-terminator-typically-this-was-a-string-returned-by-a-windows-api-call/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enhanced Split and Join functions</title>
		<link>http://www.snippetware.com/2009/10/11/enhanced-split-and-join-functions/</link>
		<comments>http://www.snippetware.com/2009/10/11/enhanced-split-and-join-functions/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 13:09:46 +0000</pubDate>
		<dc:creator>Zaur</dc:creator>
				<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.snippetware.com/?p=281</guid>
		<description><![CDATA[<p><a href="http://www.snippetware.com/2009/10/11/enhanced-split-and-join-functions/">Continue</a></p>]]></description>
			<content:encoded><![CDATA[<pre class="brush: vb">
&#039;-------------------------------------------------------------------------------------------------------------
&#039;
&#039;   Module          : modStrings
&#039;   Description     : String Utility Functions
&#039;   Author          : Quick VB
&#039;   Last Updated    : 15th April, 2000.
&#039;   Notes           : Functions may be slower esp., SplitString compared to Split function
&#039;                     But, you do need to sacrifice performance for added functionality.
&#039;                     Compile these functions into a Global Multiuse Class module in your ActiveX DLL.
&#039;
&#039;                     Requires IE 4.0 or later to be installed in order to work. (StrBPrk function)
&#039;
&#039;   Copyright Info  :
&#039;
&#039;   This module is provided AS-IS. This module can be used as a part of a compiled
&#039;   executable whether freeware or not. This module may not be posted to any web site
&#039;   or BBS or any redistributable media like CD-ROM without the consent of the author.
&#039;
&#039;   web site : http://www.quickvb.com/
&#039;
&#039;   e-mail   : esanthosh@quickvb.com
&#039;
&#039;   Revision History :
&#039;
&#039;
&#039;-------------------------------------------------------------------------------------------------------------

Option Explicit

&#039;
&#039; API Declarations
&#039;
Private Declare Function StrCSpnI Lib &quot;shlwapi&quot; Alias &quot;StrCSpnIA&quot; _
                                  (ByVal StringSearched As String, ByVal Characters As String) As Long

&#039;
&#039; Enums
&#039;
Public Enum PadType
    padLeft
    padCenter
    padRight
End Enum

Public Function SplitString(ByVal toSplit As String, _
                            Optional ByVal SplitChars As String = &quot; &quot;, _
                            Optional ByVal EscapeChar As String = &quot;\&quot;, _
                            Optional ByVal TrimStrings As Boolean = True) _
                            As String()

    &#039;--------------------------------------------------------------------------------------------------
    &#039; Arguments :
    &#039;
    &#039; toSplit               - String to be Splitted
    &#039; SplitChars            - Delimiters that are used for Splitting the String (Default : Space)
    &#039; EscapeChar            - The Escape Character (if precedes a Split Character, the Split
    &#039;                         Character is recognized as a literal) (Default : Back Slash)
    &#039; TrimStrings           - If True, trims the returned Strings in the Array, else the spaces are
    &#039;                         returned as-is.
    &#039;
    &#039; Returns   : The Splitted String Array
    &#039;
    &#039;
    &#039; Notes     :  This function is slower than the Split() counterpart. It&#039;s slows down as the length of the
    &#039;              string increases. But, it provides you two things that Split() does not provide.
    &#039;              1. It allows multiple delimiters (called SplitChars here)
    &#039;              2. What if you want to represent the delimiter itself ? For e.g., if you have
    &#039;                 - as the delimiter and you want to pass A-B as an argument ?. For this, this function
    &#039;                allows you to have one Escape Character. Instead of passing A-B, you pass the argument as
    &#039;               A\-B in this case.
    &#039;
    &#039;--------------------------------------------------------------------------------------------------

    Dim retArray() As String        &#039; The returned array of results
    Dim tempString As String        &#039; The Temporary Buffer
    Dim arrLength As Long           &#039; No. Of Splitted Parts
    Dim isEscapeChar As Boolean     &#039; Was a Escape Character found ?
    Dim FoundPos As Long            &#039; The position at which the Split Character was found

    ReDim retArray(0 To 0)          &#039; Hmm.. Looks like bad example of Initializing an array
    arrLength = -1                  &#039; No of split parts - 1

    If EscapeChar &lt;&gt; &quot;&quot; Then
        EscapeChar = Left$(EscapeChar, 1)   &#039; Only one character can be allowed as the EscapeCharacter
    End If

    If Len(toSplit) = 0 Then       &#039; No String, nothing to do ...
        SplitString = retArray
        Exit Function
    End If

    If Len(SplitChars) = 0 Then     &#039; No Splitting to be done
        SplitString = retArray
        Exit Function
    End If

    &#039;
    &#039; To avoid Redimensioning Array many times within the loop, we dimension the array to the
    &#039; maximum possible extent and waste some memory
    &#039;
    ReDim retArray(0 To 10)

    toSplit = toSplit &amp; Chr$(0)
    SplitChars = SplitChars &amp; Chr$(0)

    Do
        &#039; Use the SHLWAPI function to search for String matches
        FoundPos = StrCSpnI(toSplit, SplitChars)

        &#039;
        &#039; The String before the Split Character (if any) should be &quot;splitted&quot;
        &#039; unless there is a Escape Character preceding the Split Character
        &#039;
        FoundPos = FoundPos + 1     &#039; Remember ? C Strings start with a Zero Index

        Select Case FoundPos
            Case Len(toSplit), 0
                arrLength = arrLength + 1

                If arrLength &gt; UBound(retArray) Then
                    ReDim Preserve retArray(arrLength + 5)  &#039; Pre-Allocate more memory
                End If

                If isEscapeChar Then
                    retArray(arrLength) = tempString &amp; toSplit    &#039; Whatever is remaining
                Else
                    retArray(arrLength) = toSplit
                End If

                If TrimStrings Then retArray(arrLength) = Trim$(retArray(arrLength))
                Exit Do

            Case Is &gt; 1
                &#039; Check for Escape Character
                If Mid$(toSplit, FoundPos - 1, 1) = EscapeChar Then
                    &#039; The Split Character is intepreted as an ordinary literal.
                    isEscapeChar = True

                    &#039; Remove everything before the Escape Character for temporary storage
                    If Len(toSplit) &gt; 2 Then
                        tempString = Left$(toSplit, FoundPos - 2) &amp; Mid$(toSplit, FoundPos, 1)
                    Else        &#039; The Escape Character is the First Character
                        tempString = Mid$(toSplit, FoundPos)
                    End If

                    &#039; To avoid &quot;finding&quot; the Escaped Split Character again, we rip that part
                    If Len(toSplit) &gt; FoundPos + 1 Then
                        toSplit = Mid$(toSplit, FoundPos + 1)
                    Else
                        &#039; The String has ended with the Escaped Split Character. Now
                        &#039; this is the last part of the string that must be stored in the
                        &#039; Split Array. Just do that !
                        arrLength = arrLength + 1

                        If arrLength &gt; UBound(retArray) Then
                            ReDim Preserve retArray(arrLength + 5)  &#039; Pre-Allocate more memory
                        End If

                        retArray(arrLength) = tempString
                        If TrimStrings Then retArray(arrLength) = Trim$(retArray(arrLength))
                        Exit Do
                    End If

                Else        &#039; No Escape Character
                    arrLength = arrLength + 1

                    If arrLength &gt; UBound(retArray) Then
                        ReDim Preserve retArray(arrLength + 5)  &#039; Pre-Allocate more memory
                    End If

                    &#039; We might have left-overs of an Escaped Split Character
                    If isEscapeChar Then
                        retArray(arrLength) = tempString &amp; Left$(toSplit, FoundPos - 1)
                        isEscapeChar = False
                    Else
                        retArray(arrLength) = Left$(toSplit, FoundPos - 1)
                    End If

                    If TrimStrings Then retArray(arrLength) = Trim$(retArray(arrLength))

                    &#039; Remove that Part
                    If Len(toSplit) &gt; FoundPos + 1 Then
                        toSplit = Mid$(toSplit, FoundPos + 1)
                    Else
                        &#039; No more string to be splitted. Re-dimenstion the array.
                        Exit Do
                    End If
                End If

            Case 1
                &#039; The Split Character is at the First Position
                If Len(toSplit) &gt; 1 Then
                    toSplit = Mid$(toSplit, 2)
                Else
                    Exit Do
                End If
        End Select
    Loop

    ReDim Preserve retArray(arrLength)
    toSplit = &quot;&quot;

    SplitString = retArray

End Function

Public Function Pad(ByVal argString As String, ByVal TotalLength As Long, _
                    Optional ByVal Direction As PadType = padCenter, _
                    Optional ByVal PadCharacter As String = &quot; &quot;) As String
    &#039;
    &#039; Pads a String using another string to the specified length (Memories of XBase ?)
    &#039;

    If argString = &quot;&quot; Then Exit Function

    If TotalLength &lt;= Len(argString) Then
        Pad = Left$(argString, TotalLength)
        Exit Function
    End If

    PadCharacter = Left$(PadCharacter, 1)

    If Direction = padCenter Then
        PadCharacter = String$((TotalLength - Len(argString)) \ 2, PadCharacter)
    Else
        PadCharacter = String$(TotalLength - Len(argString), PadCharacter)
    End If

    Select Case Direction
        Case padLeft
            Pad = PadCharacter &amp; argString

        Case padRight
            Pad = argString &amp; PadCharacter

        Case Else
            Pad = PadCharacter &amp; argString &amp; PadCharacter
            If Len(Pad) &lt; TotalLength Then
                Pad = Space$(TotalLength - Len(Pad)) &amp; Pad
            End If
    End Select
End Function

Public Function JoinString(StringIn() As String, _
                           ByVal JoinCharacter As String, Optional ByVal EscapeChar As String = &quot;\&quot;) As String

    &#039;-----------------------------------------------------------------------------------------
    &#039; Arguments :  StringIn      - Array of Strings to be joined
    &#039;              JoinCharacter - Character used for joining strings in the array
    &#039;              EscapeChar    - If the Escape Character or Join Character is found in a string
    &#039;                              it is preceded by the EscapeChar. The resulting string can be
    &#039;                              used with the SplitStrting Function.
    &#039;
    &#039; Returns    : The joined String
    &#039;
    &#039; Comments   : Ever tried to Join the Strings &quot;a&quot;,&quot;b&quot;,&quot;c,d&quot; using Comma as the delimiter ?
    &#039;              You don&#039;t have to figure out which is the delimiter and which comma is the
    &#039;              literal. With JoinString(), you always know what you are getting.
    &#039;
    &#039;-----------------------------------------------------------------------------------------

    Dim I As Long, LastPos As Long

    &#039; Argument validation
    If EscapeChar = &quot;&quot; Then EscapeChar = &quot;\&quot;
    If JoinCharacter = &quot;&quot; Then JoinCharacter = &quot; &quot;
    If Len(JoinCharacter) &gt; 1 Then JoinCharacter = Left$(JoinCharacter, 1)

    For I = LBound(StringIn) To UBound(StringIn)
        If InStr(1, StringIn(I), JoinCharacter, vbTextCompare) &gt; 0 Then
            StringIn(I) = Replace(StringIn(I), JoinCharacter, EscapeChar &amp; JoinCharacter, 1, -1, vbTextCompare)
        End If

        &#039; Join &#039;em
        JoinString = JoinString &amp; StringIn(I) &amp; JoinCharacter
    Next

    &#039; Remove the last Join Character
    JoinString = Left$(JoinString, Len(JoinString) - 1)

End Function
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.snippetware.com/2009/10/11/enhanced-split-and-join-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generate GUID using the CoCreateGuid API</title>
		<link>http://www.snippetware.com/2009/10/11/generate-guid-using-the-cocreateguid-api/</link>
		<comments>http://www.snippetware.com/2009/10/11/generate-guid-using-the-cocreateguid-api/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 13:08:21 +0000</pubDate>
		<dc:creator>Zaur</dc:creator>
				<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.snippetware.com/?p=278</guid>
		<description><![CDATA[<p><a href="http://www.snippetware.com/2009/10/11/generate-guid-using-the-cocreateguid-api/">Continue</a></p>]]></description>
			<content:encoded><![CDATA[<pre class="brush: vb">
&#039; ----------------------------
&#039; Constants &amp; API Declarations
&#039; ----------------------------

Public Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type

Public Declare Function CoCreateGuid Lib &quot;OLE32.DLL&quot; (pGuid As GUID) As Long
Public Const S_OK = 0 &#039; return value from CoCreateGuid

&#039; ---------
&#039; Function
&#039; ---------

Function GetGUID() As String
    Dim lResult As Long
    Dim lguid As GUID
    Dim MyguidString As String
    Dim MyGuidString1 As String
    Dim MyGuidString2 As String
    Dim MyGuidString3 As String
    Dim DataLen As Integer
    Dim StringLen As Integer
    Dim i%

    On Error GoTo error_olemsg

    lResult = CoCreateGuid(lguid)

    If lResult = S_OK Then
        MyGuidString1 = Hex$(lguid.Data1)
        StringLen = Len(MyGuidString1)
        DataLen = Len(lguid.Data1)
        MyGuidString1 = LeadingZeros(2 * DataLen, StringLen) &amp; MyGuidString1        &#039;First 4 bytes (8 hex digits)

        MyGuidString2 = Hex$(lguid.Data2)
        StringLen = Len(MyGuidString2)
        DataLen = Len(lguid.Data2)
        MyGuidString2 = LeadingZeros(2 * DataLen, StringLen) &amp; Trim$(MyGuidString2)        &#039;Next 2 bytes (4 hex digits)

        MyGuidString3 = Hex$(lguid.Data3)
        StringLen = Len(MyGuidString3)
        DataLen = Len(lguid.Data3)
        MyGuidString3 = LeadingZeros(2 * DataLen, StringLen) &amp; Trim$(MyGuidString3)        &#039;Next 2 bytes (4 hex digits)

        GetGUID = MyGuidString1 &amp; MyGuidString2 &amp; MyGuidString3

        For i% = 0 To 7
           MyguidString = MyguidString &amp; Format$(Hex$(lguid.Data4(i%)), &quot;00&quot;)
        Next i%

        &#039;MyGuidString contains last 8 bytes of Guid (16 hex digits)
        GetGUID = GetGUID &amp; MyguidString
    Else
        GetGUID = &quot;00000000&quot; &#039; return zeros if function unsuccessful
    End If

    Exit Function

error_olemsg:
         MsgBox &quot;Error &quot; &amp; Str(Err) &amp; &quot;: &quot; &amp; Error$(Err)
         GetGUID = &quot;00000000&quot;
         Exit Function

End Function

&#039; =============================================================== &#039;

Function LeadingZeros(ExpectedLen As Integer, ActualLen As Integer) As String
    LeadingZeros = String$(ExpectedLen - ActualLen, &quot;0&quot;)
End Function
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.snippetware.com/2009/10/11/generate-guid-using-the-cocreateguid-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

