⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringtools.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Option Strict On
Public Class StringTools
    Const strNum1 As String = "One"
    Const strNum2 As String = "Two"
    Const strNum3 As String = "Three"
    Const strNum4 As String = "Four"
    Const strNum5 As String = "Five"
    Const strNum6 As String = "Six"
    Const strNum7 As String = "Seven"
    Const strNum8 As String = "Eight"
    Const strNum9 As String = "Nine"
    Const strNum10 As String = "Ten"
    Const strNum11 As String = "Eleven"
    Const strNum12 As String = "Twelve"
    Const strNum13 As String = "Thirteen"
    Const strNum14 As String = "Fourteen"
    Const strNum15 As String = "Fifteen"
    Const strNumteen As String = "teen"
    Const strNum20 As String = "Twenty"
    Const strNum30 As String = "Thirty"
    Const strNum40 As String = "Fourty"
    Const strNum50 As String = "Fifty"
    Const strNum60 As String = "Sixty"
    Const strNum70 As String = "Seventy"
    Const strNum80 As String = "Eighty"
    Const strNum90 As String = "Ninety"

    Enum clsToolsPadding
        clsToolsCommas
        clsToolsSpaces
        clsToolsDashes
    End Enum

    Enum clsToolsDelimit
        clsToolsNone
        clsTools1Asterisk
        clsTools3Asterisks
    End Enum

    Enum clsToolsCase
        clsToolsCaps
        clsToolsLower
        clsToolsUpper
    End Enum

    'local variable(s) to hold property value(s)
    Private mvarUseCase As clsToolsCase 'local copy
    Private mvarUseDelimiter As clsToolsDelimit 'local copy
    Private mvarUsePadding As clsToolsPadding 'local copy

    Public Function Num2String(ByVal number As Decimal) As String
        Dim biln As Decimal, miln As Decimal, thou As Decimal, hund As Decimal
        Dim ten As Integer, units As Integer
        Dim strNumber As String

        If number > 999999999999.99 Then
            Num2String = "***"
            Exit Function
        End If

        biln = Fix(number / 1000000000)
        If biln > 0 Then strNumber = FormatNum(biln) & " Billion" & Pad()
        miln = Fix((number - biln * 1000000000) / 1000000)
        If miln > 0 Then strNumber = strNumber & FormatNum(miln) & " Million" & Pad()
        thou = Fix((number - biln * 1000000000 - miln * 1000000) / 1000)
        If thou > 0 Then strNumber = strNumber & FormatNum(thou) & " Thousand" & Pad()
        hund = Fix(number - biln * 1000000000 - miln * 1000000 - thou * 1000)
        If hund > 0 Then strNumber = strNumber & FormatNum(hund)
        If Right(strNumber, 1) = "," Then strNumber = Left(strNumber, Len(strNumber) - 1)
        If Left(strNumber, 1) = "," Then strNumber = Right(strNumber, Len(strNumber) - 1)
        If number <> Int(number) Then
            strNumber = strNumber & FormatDecimal((number - Int(number)) * 100)
        Else
            ' the following loop is necessary for the proper conversion
            ' of amount like 32,100,000 or 1,000,000,000.
            While strNumber.EndsWith(", ")
                strNumber = Left(strNumber, strNumber.Length - 2) & " "
            End While
            strNumber = strNumber & " dollars"
        End If
        Num2String = Delimit(SetCase(strNumber))
    End Function

    Public Property UsePadding() As clsToolsPadding
        Get
            Return (mvarUsePadding)
        End Get
        Set(ByVal Value As clsToolsPadding)
            mvarUsePadding = Value
        End Set

    End Property


    Public Property UseDelimiter() As clsToolsDelimit
        Get
            Return (mvarUseDelimiter)
        End Get
        Set(ByVal Value As clsToolsDelimit)
            mvarUseDelimiter = Value
        End Set
    End Property



    Public Property UseCase() As clsToolsCase
        Get
            Return (mvarUseCase)
        End Get
        Set(ByVal Value As clsToolsCase)
            mvarUseCase = Value
        End Set
    End Property


    Private Function FormatNum(ByVal num As Decimal) As String
        Dim digit100 As Decimal, digit10 As Decimal, digit1 As Decimal
        Dim strNum As String

        digit100 = Int(num / 100)
        If digit100 > 0 Then strNum = Format100(digit100)
        digit10 = Fix((num - digit100 * 100))
        If digit10 > 0 Then
            If strNum <> "" Then
                strNum = strNum & " And " & Format10(digit10)
            Else
                strNum = Format10(digit10)
            End If
        End If
        FormatNum = strNum
    End Function

    Private Function Format100(ByVal num As Decimal) As String
        Dim str100 As String
        str100 = Format1(num) & " Hundred"
        Format100 = str100
    End Function

    Private Function Format10(ByVal num As Decimal) As String
        Dim strNum As String
        If num < 10 Then
            Format10 = Format1(num)
            Exit Function
        End If
        If num < 20 Then
            Select Case num
                Case 10
                    Format10 = strNum10
                    Exit Function
                Case 11
                    Format10 = strNum11
                    Exit Function
                Case 12
                    Format10 = strNum12
                    Exit Function
                Case 13
                    Format10 = strNum13
                    Exit Function
                Case 14
                    Format10 = strNum14
                    Exit Function
                Case 15
                    Format10 = strNum15
                    Exit Function
                Case Else
                    Format10 = Format1(num Mod 10) & strNumteen
            End Select
            Exit Function
        Else
            Select Case Int(num / 10) * 10
                Case 20 : strNum = strNum20
                Case 30 : strNum = strNum30
                Case 40 : strNum = strNum40
                Case 50 : strNum = strNum50
                Case 60 : strNum = strNum60
                Case 70 : strNum = strNum70
                Case 80 : strNum = strNum80
                Case 90 : strNum = strNum90
            End Select
            Format10 = strNum & " " & Format1(num Mod 10)
        End If
    End Function

    Private Function Format1(ByVal num As Decimal) As String
        Select Case num
            Case 1 : Format1 = strNum1
            Case 2 : Format1 = strNum2
            Case 3 : Format1 = strNum3
            Case 4 : Format1 = strNum4
            Case 5 : Format1 = strNum5
            Case 6 : Format1 = strNum6
            Case 7 : Format1 = strNum7
            Case 8 : Format1 = strNum8
            Case 9 : Format1 = strNum9
        End Select
    End Function

    Private Function FormatDecimal(ByVal num As Decimal) As String
        FormatDecimal = " dollars and " & Format10(num) & " cents"
    End Function

    Private Function SetCase(ByVal nString As String) As String
        Select Case mvarUseCase
            Case clsToolsCase.clsToolsUpper : SetCase = nString.ToLower
            Case clsToolsCase.clsToolsLower : SetCase = nString.ToUpper
            Case clsToolsCase.clsToolsCaps : SetCase = nString
        End Select
    End Function

    Private Function Pad() As String
        Select Case mvarUsePadding
            Case clsToolsPadding.clsToolsSpaces : Pad = " "
            Case clsToolsPadding.clsToolsDashes : Pad = "-"
            Case clsToolsPadding.clsToolsCommas : Pad = ", "
        End Select
    End Function

    Private Function Delimit(ByVal nString As String) As String
        Select Case mvarUseDelimiter
            Case clsToolsDelimit.clsToolsNone : Delimit = nString
            Case clsToolsDelimit.clsTools1Asterisk : Delimit = "*" & nString & "*"
            Case clsToolsDelimit.clsTools3Asterisks : Delimit = "***" & nString & "***"
        End Select
    End Function

    Public Function ExtractFileName(ByVal PathFileName As String) As String
        Dim delimiterPosition As Integer
        delimiterPosition = PathFileName.LastIndexOf("\")
        If delimiterPosition > 0 Then
            Return PathFileName.Substring(delimiterPosition + 1)
        Else
            Return PathFileName
        End If
    End Function

    Public Function ExtractPathName(ByVal PathFileName As String) As String
        Dim delimiterPosition As Integer
        delimiterPosition = PathFileName.LastIndexOf("\")
        If delimiterPosition > 0 Then
            Return PathFileName.Substring(0, delimiterPosition)
        Else
            Return ""
        End If
    End Function

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -