我前段时间写了一个方法来做你需要的。
用法:
· 对于句子大小写(TitleCase)
TextBox1.SelectedText =
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Title)
· ToggleCase
TextBox1.SelectedText =
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Toggle)
· 正确大小写(WordCase)
TextBox1.SelectedText =
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Word)
· 小写
TextBox1.SelectedText =
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Lower)
· 大写
TextBox1.SelectedText =
StringRenamer.Rename(TextBox1.SelectedText, StringRenamer.StringCase.Upper)
#Region " String Renamer "
' [ String Renamer ]
'
' // By Elektro H@cker
'
' Examples :
'
' MsgBox(StringRenamer.Rename("Hello World!", StringRenamer.StringCase.Upper))
' MsgBox(StringRenamer.Rename("Hello World!", StringRenamer.StringCase.Upper, New System.Text.RegularExpressions.Regex("\s+"), "-", RegexOptions.None))
Public Class StringRenamer
Private Shared output As String = String.Empty
Public Enum StringCase As Short
''' <summary>
''' LowerCase
'''
''' [Example]
''' Input : ABCDEF
''' Output: abcdef
''' </summary>
Lower = 0
''' <summary>
''' UpperCase.
'''
''' [Example]
''' Input : abcdef
''' Output: ABCDEF
''' </summary>
Upper = 1
''' <summary>
''' TitleCase.
'''
''' [Example]
''' Input : abcdef
''' Output: Abcdef
''' </summary>
Title = 2
''' <summary>
''' WordCase.
'''
''' [Example]
''' Input : abc def
''' Output: Abc Def
''' </summary>
Word = 3
''' <summary>
''' CamelCase (With first letter to LowerCase).
'''
''' [Example]
''' Input : ABC DEF
''' Output: abcDef
''' </summary>
Camel_Lower = 4
''' <summary>
''' CamelCase (With first letter to UpperCase).
'''
''' [Example]
''' Input : ABC DEF
''' Output: AbcDef
''' </summary>
Camel_Upper = 5
''' <summary>
''' MixedCase (With first letter to LowerCase).
'''
''' [Example]
''' Input : ab cd ef
''' Output: aB Cd eF
''' </summary>
Mixed_TitleLower = 6
''' <summary>
''' MixedCase (With first letter to UpperCase).
'''
''' [Example]
''' Input : ab cd ef
''' Output: Ab cD Ef
''' </summary>
Mixed_TitleUpper = 7
''' <summary>
''' MixedCase (With first letter of each word to LowerCase).
'''
''' [Example]
''' Input : ab cd ef
''' Output: aB cD eF
''' </summary>
Mixed_Word_Lower = 8
''' <summary>
''' MixedCase (With first letter of each word to UpperCase).
'''
''' [Example]
''' Input : ab cd ef
''' Output: Ab Cd Ef
''' </summary>
Mixed_Word_Upper = 9
''' <summary>
''' ToggleCase.
'''
''' [Example]
''' Input : abc def ghi
''' Output: aBC dEF gHI
''' </summary>
Toggle = 10
''' <summary>
''' Inverts the characters.
'''
''' [Example]
''' Input : Hello World!
''' Output: hELLO wORLD!
''' </summary>
Inverted = 11
End Enum
''' <summary>
''' Rename a string to the specified StringCase.
''' </summary>
Public Shared Function Rename(ByVal Text As String,
ByVal sCase As StringCase) As String
Select Case sCase
Case StringCase.Lower
Return Text.ToLower
Case StringCase.Upper
Return Text.ToUpper
Case StringCase.Title
Return Char.ToUpper(Text.First) & Text.Substring(1).ToLower
Case StringCase.Word
Return Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Text.ToLower)
Case StringCase.Camel_Lower
Return Char.ToLower(Text.First) &
Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Text.ToLower).
Replace(" ", "").
Substring(1)
Case StringCase.Camel_Upper
Return Char.ToUpper(Text.First) &
Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(Text.ToLower).
Replace(" ", "").
Substring(1)
Case StringCase.Mixed_TitleLower
output = String.Empty
For X As Integer = 0 To Text.Length - 1 Step 2
Try
output &= Char.ToLower(Text(X)) &
Char.ToUpper(Text(X + 1))
Catch ex As IndexOutOfRangeException
output &= Char.ToLower(Text(X))
End Try
Next X
Return output
Case StringCase.Mixed_TitleUpper
output = String.Empty
For X As Integer = 0 To Text.Length - 1 Step 2
Try
output &= Char.ToUpper(Text(X)) &
Char.ToLower(Text(X + 1))
Catch ex As IndexOutOfRangeException
output &= Char.ToUpper(Text(X))
End Try
Next X
Return output
Case StringCase.Mixed_Word_Lower
output = String.Empty
For Each token As String In Text.Split
output &= StringRenamer.Rename(token, StringCase.Mixed_TitleLower) & " "
Next token
Return output
Case StringCase.Mixed_Word_Upper
output = String.Empty
For Each token As String In Text.Split
output &= StringRenamer.Rename(token, StringCase.Mixed_TitleUpper) & " "
Next token
Return output
Case StringCase.Toggle
output = String.Empty
For Each token As String In Text.Split
output &= Char.ToLower(token.First) & token.Substring(1).ToUpper & " "
Next token
Return output
Case StringCase.Inverted
output = String.Empty
For Each c As Char In Text
output &= If(Char.IsLower(c),
Char.ToUpper(c),
Char.ToLower(c))
Next c
Return output
Case Else
Return Nothing
End Select
End Function
''' <summary>
''' Rename a string to the specified StringCase,
''' Also find and replace text after rename.
''' </summary>
Public Shared Function Rename(ByVal Text As String,
ByVal sCase As StringCase,
ByVal FindWhat As System.Text.RegularExpressions.Regex,
ByVal ReplaceWith As String,
ByVal RegExIgnoreCase As System.Text.RegularExpressions.RegexOptions) As String
Return System.Text.RegularExpressions.
Regex.Replace(StringRenamer.Rename(Text, sCase), FindWhat.ToString, ReplaceWith, RegExIgnoreCase)
End Function
End Class
#End Region