VB.NET, Custom String Methods

In this article I will explain you about the Custom String Methods using VB.NET
  • 2920
Download Files:
 

Below are some custom string methods for VB.NET that are found in various scripting languages: PCase, Replace, ToSingleSpace, CharCount, Reverse, Left, Right, and IsPalindrome. These methods are useful in common string operations. Since the .NET Framework does not natively include these methods, you can use the methods in to build your own "super string" class. Let's start with the code, then explain it in detail.

Auxiliary Custom String Methods

    Friend Class MyString
        Public Shared Sub Main()
            Dim strData As String = "WeLcOmE tO VB.NET eNjoy FoLkS"
            Console.WriteLine("String Value: {0}", strData)
            Console.WriteLine("PCase Equivalent: {0}", PCase(strData))
            Console.WriteLine("Reverse Equivalent: {0}", Reverse(strData))
            Console.WriteLine("Is 'rotator' PalinDrome: {0}", IsPalindrome("rotator"))
            Console.WriteLine("Is 'VB.NET' PalinDrome: {0}", IsPalindrome("VB.NET"))
            Console.WriteLine("Left(string,5): {0}", Left(strData, 5))
            Console.WriteLine("Right(String,6): {0}", Right(strData, 6))
            Console.WriteLine("CharCount(Charcount,c):{0}", CharCount("Charcount", "C"))
            Console.WriteLine("CharCount(CharCount,c,true):{0}", CharCount("Charcount", "C", True))
            Console.WriteLine("CharCount(CharCount,d,true):{0}", CharCount("Charcount", "d", True))
            Console.WriteLine("ToSingleSpace('welcome to VB.NET'): {0}", ToSingleSpace("welcome to VB.NET"))
            Console.WriteLine("Replace(aaaaaa,aa,a):{0}", Replace("aaaaaa", "aa", "a"))
            Console.ReadLine()
        End Sub

        Public
Shared Function PCase(ByVal strParam As String) As String
            Dim
strProper As String = strParam.Substring(0, 1).ToUpper()
            strParam = strParam.Substring(1).ToLower()
            Dim strPrev As String = ""
            For iIndex As Integer = 0 To strParam.Length - 1
                If iIndex > 1 Then
                    strPrev = strParam.Substring(iIndex - 1, 1)
                End If
                If
strPrev.Equals(" ") OrElse strPrev.Equals(vbTab) OrElse strPrev.Equals(vbLf) OrElse strPrev.Equals(".") Then
                    strProper &= strParam.Substring(iIndex, 1).ToUpper()
                Else
                    strProper &= strParam.Substring(iIndex, 1)
                End If
                Next
iIndex
                Return strProper
        End Function

        Public
Shared Function Replace(ByVal strText As String, ByVal strFind As String, ByVal strReplace As String) As String
            Dim
iPos As Integer = strText.IndexOf(strFind)
            Dim strReturn As String = ""
            Do While iPos <> -1
                strReturn &= strText.Substring(0, iPos) & strReplace
                strText = strText.Substring(iPos + strFind.Length)
                iPos = strText.IndexOf(strFind)
            Loop
            If
strText.Length > 0 Then
                strReturn &= strText
            End If
            Return
strReturn
        End Function

        Public
Shared Function ToSingleSpace(ByVal strParam As String) As String
            Dim
iPosition As Integer = strParam.IndexOf(" ")
            If iPosition = -1 Then
                Return
strParam
            Else
                Return
ToSingleSpace(strParam.Substring(0, iPosition) & strParam.Substring(iPosition + 1))
            End If
        End
Function

        Public
Shared Function CharCount(ByVal strSource As String, ByVal strToCount As String) As Integer
            Dim
iCount As Integer = 0
            Dim iPos As Integer = strSource.IndexOf(strToCount)
            Do While iPos <> -1
                iCount += 1
                strSource = strSource.Substring(iPos + 1)
                iPos = strSource.IndexOf(strToCount)
            Loop
            Return
iCount
        End Function

        Public
Shared Function CharCount(ByVal strSource As String, ByVal strToCount As String, ByVal IgnoreCase As Boolean) As Integer
            If
IgnoreCase Then
                Return
CharCount(strSource.ToLower(), strToCount.ToLower())
            Else
                Return
CharCount(strSource, strToCount)
            End If
        End
Function

        Public
Shared Function Reverse(ByVal strParam As String) As String
            If
strParam.Length = 1 Then
                Return
strParam
            Else
                Return
Reverse(strParam.Substring(1)) & strParam.Substring(0, 1)
            End If
        End
Function

        Public
Shared Function Left(ByVal strParam As String, ByVal iLen As Integer) As String
            If
iLen > 0 Then
                Return
strParam.Substring(0, iLen)
            Else
                Return
strParam
            End If
        End
Function

        Public
Shared Function Right(ByVal strParam As String, ByVal iLen As Integer) As String
            If
iLen > 0 Then
                Return
strParam.Substring(strParam.Length - iLen, iLen)
            Else
                Return
strParam
            End If
        End
Function

        Public
Shared Function IsPalindrome(ByVal strParam As String) As Boolean
            Dim
iLength, iHalfLen As Integer
            iLength = strParam.Length - 1
            iHalfLen = iLength \ 2
            For iIndex As Integer = 0 To iHalfLen
                If strParam.Substring(iIndex, 1) <> strParam.Substring(iLength - iIndex, 1) Then
                    Return
False
                End
If
            Next
iIndex
            Return True
        End
Function
    End
Class

Output of above code:

4.gif

The PCase method converts a string to proper case, capitalizing each word's first character. It distinguishes words using whitespace characters such as space, tab, line feed, and carriage return characters ( ' ', '\t', '\n', '\r'). Its usage is PCase(String).

The Replace method replaces strings with string phrases and characters. This function finds characters passed in the second argument within the source string in the first argument and replaces them with characters in the third argument. Its usage is Replace(Source, Find, Replacement). For example, Replace("abc","b","d") will return "adc".

The ToSingleSpace function converts multiple whitespace characters to single whitespace characters. Its usage is ToSingleSpace(SourceString). For example, ToSingleSpace("Welcome to VB.NET") will return "Welcome to VB.NET".

The CharCount method returns the number of occurrences of a substring in the main string. CharCount has two overloads: one for case-sensitive operations and the other for case-insensitive operations. For case-insensitive operations, CharCount simply converts both string and substring parameters to full lowercase, then calls the case-sensitive CharCount method. The CharCount method can be useful for string-parsing operations. CharCount usages are CharCount(Source, Find) or CharCount(Source, Find, true). For example, CharCount("aaaaac", "a") and CharCount("aaaaac","A", true) will return 5, but CharCount("aaaaac", "A") and CharCount("aaaaac", "A", false) will return 0.

The Reverse method reverses and returns the characters in a string argument. Its usage is Reverse(Source). For example, Reverse("abc") will return cba.

The Left method returns a certain number of characters from the beginning, or left side, of the string. Its usage is Left(Source, CharCount). For example, Left("Welcome", 3) will return Wel.

The Right method returns a certain number of characters from the end, or right side, of the string. Its usage is Right(Source, CharCount). For example, Right("Welcome", 2) will return me.

The IsPalindrome function returns whether the passed string is a palindrome reading the same backward as forward. Its usage is IsPalindrome(Source). For example, IsPalindrome("abc") will return false, whereas IsPalindrome("121") will return true.

Conclusion

Hope this article would have helped you in understanding Custom String Methods using VB.NET

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.