VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A collection of string manipulation functions

by Tanwani Anyangwe (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 11th April 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

A collection of string manipulation functions

Rate A collection of string manipulation functions



        Const CharSet As String = "0123456789" & "ABCDEFGHIJKLMNOPQRSTUVWXYZ" _
            & "abcdefghijklmnopqrstuvwxyz"
        Dim strRandom As String
        Randomize()
        Do Until Len(strRandom) >= lngPasswordLen
            strRandom = strRandom & Mid(CharSet, Int((Len(CharSet) * Rnd()) + 1), 1)
        Loop
        GetRandomString = strRandom
    End Function

    Public Function PCase(ByVal strInput As String) As String
        Dim intPosition As Integer
        Dim intSpace As Integer
        Dim strOutput As String
        intPosition = 1
        Do While InStr(intPosition, strInput, " ", 1) <> 0
            intSpace = InStr(intPosition, strInput, " ", 1)
            strOutput = strOutput & UCase(Mid(strInput, intPosition, 1))
            strOutput = strOutput & LCase(Mid(strInput, intPosition + 1, intSpace - intPosition))

            intPosition = intSpace + 1
        Loop
        strOutput = strOutput & UCase(Mid(strInput, intPosition, 1))
        strOutput = strOutput & LCase(Mid(strInput, intPosition + 1))
        PCase = strOutput
    End Function

    Function SubStrBefore(ByVal strInput As String, ByVal strSearch As String, _
        Optional ByVal intStartIndex As Integer = 0) As String
        Dim intIndex As Integer = _
            strInput.ToLower.IndexOf(strSearch.ToLower, IIf(intStartIndex < 0, 0, intStartIndex))
        If intIndex > -1 Then
            SubStrBefore = strInput.Substring(intStartIndex, intIndex - intStartIndex)
        Else
            SubStrBefore = strInput.Substring(intStartIndex)
        End If
    End Function

    Function SubStrAfter(ByVal strInput As String, ByVal strSearch As String, _
        Optional ByVal intStartIndex As Integer = 0) As String
        Dim intIndex As Integer = _
            strInput.ToLower.IndexOf(strSearch.ToLower, IIf(intStartIndex < 0, 0, intStartIndex))
        If intIndex > -1 Then
            SubStrAfter = strInput.Substring(intIndex + strSearch.Length)
        Else
            SubStrAfter = ""
        End If
    End Function

    Function LastSubStrBefore(ByVal strInput As String, ByVal strSearch As String) As String
        Dim intStopIndex As Integer = strInput.ToLower.IndexOf(strSearch.ToLower)
        If intStopIndex > -1 Then
            strInput = strInput.Substring(0, intStopIndex)
        End If
        LastSubStrBefore = strInput
    End Function

    Function LastSubStrAfter(ByVal strInput As String, ByVal strSearch As String) As String
        LastSubStrAfter = SubStrAfter(strInput, strSearch, strInput.ToLower.LastIndexOf(strSearch.ToLower))
    End Function

    Public Function XmlEncode(ByVal strText As String) As String
        Dim aryChars() As Integer = {38, 60, 62, 34, 61, 39}
        Dim i As Integer
        For i = 0 To aryChars.Length - 1
            strText = strText.Replace(Chr(aryChars(i)), "&#" & aryChars(i) & ";")
        Next
        XmlEncode = strText
    End Function

    Public Function SQLEncode(ByVal strText As String) As String
        If InStr(strText, "'") > 0 Then
            SQLEncode = strText.Replace("'", "''")
        Else
            SQLEncode = strText
        End If
    End Function

    Function Strip(ByVal strText As String) As String
        Dim chAti As Integer
        If strText.Length < 1 Then
            Exit Function
        End If
        chAti = Asc(Left(strText, 1))
        While Not (chAti > 47 And chAti < 123)
            strText = Mid(strText, 2)
            If strText.Length < 1 Then
                Exit Function
            End If
            chAti = Asc(Left(strText, 1))
        End While
        chAti = Asc(Right(strText, 1))
        While Not (chAti > 47 And chAti < 123)
            strText = Left(strText, Len(strText) - 1)
            If strText.Length < 1 Then
                Exit Function
            End If
            chAti = Asc(Right(strText, 1))
        End While
        Strip = strText
    End Function

Download this snippet    Add to My Saved Code

A collection of string manipulation functions Comments

No comments have been posted about A collection of string manipulation functions. Why not be the first to post a comment about A collection of string manipulation functions.

Post your comment

Subject:
Message:
0/1000 characters