RealWorldDevelopers
5/24/2016 - 12:15 AM

Common Formatters

Common Formatters

    ''' <summary>
    ''' Removea all non-Digits from a String
    ''' </summary>
    ''' <param name="sValue">String to Parse as String</param>
    ''' <returns>Parsed String as String</returns>
    ''' <remarks></remarks>
    Public Function RemoveAllNonDigits(ByVal sValue As String) As String
        Dim sResult As String = ""
        Try
            For Each ch As Char In sValue
                If Char.IsDigit(ch) Then
                    sResult &= ch
                End If
            Next
        Catch ex As Exception
            sResult = sValue
        End Try
        Return sResult
    End Function

    ''' <summary>
    ''' Formats a Phone Number 
    ''' </summary>
    ''' <param name="sNumToBeFormatted">Phone Number to Format as String</param>
    ''' <returns>Formated Phone Number as String</returns>
    ''' <remarks></remarks>
    Public Function FormatPhoneNumber(ByVal sNumToBeFormatted As String) As String
        Dim sResult As String = ""
        Dim sTemp As String = ""
        Dim iNumberLength As Integer 'Used for the Phone Number length
        Try
            sTemp = RemoveAllNonDigits(sNumToBeFormatted)
            'Length of the phone number.
            iNumberLength = Len(sTemp)
            Select Case iNumberLength
                Case 7  'Format : #######
                    sResult = sTemp.Substring(0, 3) & "-" & sTemp.Substring(3, 4)
                Case 10 'Format : ##########
                    sResult = "(" & sTemp.Substring(0, 3) & ") " & sTemp.Substring(3, 3) & "-" & sTemp.Substring(6, 4)
                Case Else
                    'Return Value Passed
                    sResult = sNumToBeFormatted

            End Select
        Catch ex As Exception
            sResult = sNumToBeFormatted
        End Try
        Return sResult
    End Function

    ''' <summary>
    ''' Format Date as MM/DD/YYYY
    ''' </summary>
    ''' <param name="strDate">Date to Format as Date</param>
    ''' <returns>Date Formatted as String</returns>
    ''' <remarks></remarks>
    Public Function FormatDate(ByVal strDate As String) As String
        Dim sResult As String = ""
        Dim strTemp As String = ""
        Try
            If strDate.Trim = "" Then Exit Try
            If Not IsDate(strDate) Then
                strTemp = RemoveAllNonDigits(strDate).PadRight(8, " ")
                If strTemp.Length > 8 Then strTemp = strTemp.Substring(0, 8)
                sResult = strTemp.Substring(4, 2) & "/" & strTemp.Substring(6, 2) & "/" & strTemp.Substring(0, 4)
                If IsDate(sResult) Then
                    sResult = Format(CDate(sResult), "MM/dd/yyyy")
                Else
                    sResult = strDate
                End If
            Else
                sResult = Format(CDate(strDate), "MM/dd/yyyy")
            End If
        Catch ex As Exception
            sResult = strDate
        End Try
        Return sResult
    End Function

    ''' <summary>
    ''' Format Time as HH:MM:SS
    ''' </summary>
    ''' <param name="strTime">Time to Format as Date</param>
    ''' <returns>Time Formatted as String</returns>
    ''' <remarks></remarks>
    Public Function FormatTime(ByVal strTime As String) As String
        Dim sResult As String = ""
        Dim strTemp As String = ""
        Try
            If strTime.Trim = "" Then Exit Try
            If IsDate(strTime) Then
                sResult = strTime
            Else
                strTemp = RemoveAllNonDigits(strTime).PadRight(6, "0")
                If strTemp.Length > 6 Then strTemp = strTemp.Substring(strTemp.Length - 6, 6)
                If strTemp = "240000" Then strTemp = "000000"
                sResult = strTemp.Substring(0, 2) & ":" & strTemp.Substring(2, 2) & ":" & strTemp.Substring(4, 2)
            End If
            sResult = Format(CDate(sResult), "hh:mm:ss")
        Catch ex As Exception
            sResult = strTime
        End Try
        Return sResult
    End Function

    ''' <summary>
    ''' Format SSN as ###-##-####
    ''' </summary>
    ''' <param name="sNumToBeFormatted">SSN to Format as String</param>
    ''' <returns>Formatted SSN as String</returns>
    ''' <remarks></remarks>
    Public Function FormatSSN(ByVal sNumToBeFormatted As String) As String
        Dim sResult As String = ""
        Dim sTemp As String = ""
        Dim iNumberLength As Integer 'Used for the SSN length
        Try
            sTemp = RemoveAllNonDigits(sNumToBeFormatted)
            'Length of the SSN.
            iNumberLength = Len(sTemp)
            Select Case iNumberLength
                Case 9  'Format : #########
                    sResult = sTemp.Substring(0, 3) & "-" & sTemp.Substring(3, 2) & "-" & sTemp.Substring(5, 4)
                Case Else
                    'Return Value Passed
                    sResult = sNumToBeFormatted

            End Select
        Catch ex As Exception
            sResult = sNumToBeFormatted
        End Try
        Return sResult
    End Function
    
        Public    Function   FormatZipCode(   ByVal   sNumToBeFormatted   As    String   )   As    String  
         Dim   sResult   As    String    =    ""  
         Dim   sTemp   As    String    =    ""  
         Dim   iNumberLength   As    Integer    'Used for the SSN length  
         Try  
         sTemp   =   RemoveAllNonDigits(sNumToBeFormatted)  
            'Length of the SSN.  
         iNumberLength   =   Len(sTemp)  
            Select    Case   iNumberLength  
               Case   5     'Format : #####  
               sResult   =   sTemp  
               Case   9     'Format : #########  
               sResult   =   sTemp.Substring(0, 5) &   "-"   & sTemp.Substring(5, 4)  
               Case    Else  
                  'Return Value Passed  
               sResult   =   sNumToBeFormatted  

         End   Select  
         Catch   ex   As   Exception  
         sResult   =   sNumToBeFormatted  
      End   Try  
         Return   sResult  
      End Function 
      
      Function   FixedLengthString(   ByVal   value   As    String   ,   ByVal   FinishedLength   As    Integer   ,   ByVal   PadWith   As    String   , _  
          Optional    ByVal   RightSide   As    Boolean    =    False   )   As    String  

     Dim   result   As    String    =   value  
     Dim   length   As    Integer    =   value.Length  

     Try  

     Select    Case   length  
      Case    Is    >   FinishedLength  
         If   RightSide   Then  
      result   =   value.Substring(length   -   FinishedLength)  
         Else  
      result   =   value.Substring(0, FinishedLength)  
         End If  
      Case   FinishedLength  
      result   =   value  
      Case    Is    <   FinishedLength  
         If   RightSide   Then  
      result   =   value.PadRight(FinishedLength, PadWith)  
         Else  
      result   =   value.PadLeft(FinishedLength, PadWith)  
         End If  
      Case    Else  
      result   =   value  
   End   Select  

     Catch   ex   As   Exception  
   result   =   value  
  End   Try  

     Return   result  

 End Function