Does anyone have a good Proper Case algorithm

前端 未结 13 1211
一生所求
一生所求 2020-12-15 04:03

Does anyone have a trusted Proper Case or PCase algorithm (similar to a UCase or Upper)? I\'m looking for something that takes a value such as \"GEORGE BURDELL\"

13条回答
  •  一生所求
    2020-12-15 05:00

    I use this as the textchanged event handler of text boxes. Support entry of "McDonald"

    Public Shared Function DoProperCaseConvert(ByVal str As String, Optional ByVal allowCapital As Boolean = True) As String
        Dim strCon As String = ""
        Dim wordbreak As String = " ,.1234567890;/\-()#$%^&*€!~+=@"
        Dim nextShouldBeCapital As Boolean = True
    
        'Improve to recognize all caps input
        'If str.Equals(str.ToUpper) Then
        '    str = str.ToLower
        'End If
    
        For Each s As Char In str.ToCharArray
    
            If allowCapital Then
                strCon = strCon & If(nextShouldBeCapital, s.ToString.ToUpper, s)
            Else
                strCon = strCon & If(nextShouldBeCapital, s.ToString.ToUpper, s.ToLower)
            End If
    
            If wordbreak.Contains(s.ToString) Then
                nextShouldBeCapital = True
            Else
                nextShouldBeCapital = False
            End If
        Next
    
        Return strCon
    End Function
    

提交回复
热议问题