#Region " String to Case "
' [ String to Case Function ]
'
' // By Elektro H@cker
'
' Examples :
' MsgBox(String_To_Case("THiS is a TeST", StringCase.Titlecase))
' MsgBox(String_To_Case("THiS is a TeST", StringCase.DelimitedCase_Lower, ";"))
' Var = String_To_WordCase(Var, StringCase.LowerCase)
Public Enum StringCase
LowerCase
UpperCase
Titlecase
WordCase
CamelCase_First_Lower
CamelCase_First_Upper
MixedCase_First_Lower
MixedCase_First_Upper
MixedCase_Word_Lower
MixedCase_Word_Upper
DelimitedCase_Lower
DelimitedCase_Mixed_Word_Lower
DelimitedCase_Mixed_Word_Upper
DelimitedCase_Title
DelimitedCase_Upper
DelimitedCase_Word
End Enum
Private Function String_To_Case(ByVal str As String, _
ByVal StringCase As StringCase, _
Optional ByVal Delimiter As String = "-") As String
Select Case StringCase
Case StringCase.LowerCase
Return str.ToLower
Case StringCase.UpperCase
Return str.ToUpper
Case StringCase.Titlecase
Return Char.ToUpper(str(0)) + StrConv(str.Substring(1), VbStrConv.Lowercase)
Case StringCase.WordCase
Return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str)
Case StringCase.CamelCase_First_Lower
Return Char.ToLower(str(0)) & _
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str).Replace(" ", "").Substring(1)
Case StringCase.CamelCase_First_Upper
Return Char.ToUpper(str(0)) & _
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str).Replace(" ", "").Substring(1)
Case StringCase.MixedCase_First_Lower
Dim MixedString As String = Nothing
For X As Integer = 0 To str.Length - 1
Dim c As Char = str(X)
If (X / 2).ToString.Contains(",") Then _
MixedString += c.ToString.ToUpper _
Else MixedString += c.ToString.ToLower
Next
Return MixedString
Case StringCase.MixedCase_First_Upper
Dim MixedString As String = Nothing
For X As Integer = 0 To str.Length - 1
Dim c As Char = str(X)
If (X / 2).ToString.Contains(",") Then _
MixedString += c.ToString.ToLower _
Else MixedString += c.ToString.ToUpper
Next
Return MixedString
Case StringCase.MixedCase_Word_Lower
Dim MixedString As String = Nothing
Dim Count As Integer = 1
For X As Integer = 0 To str.Length - 1
Dim c As Char = str(X)
If Not c = " " Then Count += 1 Else Count = 1
If (Count / 2).ToString.Contains(",") Then _
MixedString += c.ToString.ToUpper _
Else MixedString += c.ToString.ToLower
Next
Return MixedString
Case StringCase.MixedCase_Word_Upper
Dim MixedString As String = Nothing
Dim Count As Integer = 1
For X As Integer = 0 To str.Length - 1
Dim c As Char = str(X)
If Not c = " " Then Count += 1 Else Count = 1
If (Count / 2).ToString.Contains(",") Then _
MixedString += c.ToString.ToLower _
Else MixedString += c.ToString.ToUpper
Next
Return MixedString
Case StringCase.DelimitedCase_Lower
Dim rgx As New System.Text.RegularExpressions.Regex("\s+")
Return rgx.Replace(str.ToLower, Delimiter)
Case StringCase.DelimitedCase_Upper
Dim rgx As New System.Text.RegularExpressions.Regex("\s+")
Return rgx.Replace(str.ToUpper, Delimiter)
Case StringCase.DelimitedCase_Title
Dim rgx As New System.Text.RegularExpressions.Regex("\s+")
Return rgx.Replace(Char.ToUpper(str(0)) + StrConv(str.Substring(1), VbStrConv.Lowercase), Delimiter)
Case StringCase.DelimitedCase_Word
Dim rgx As New System.Text.RegularExpressions.Regex("\s+")
Return rgx.Replace(System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str), Delimiter)
Case StringCase.DelimitedCase_Mixed_Word_Lower
Dim MixedString As String = Nothing
Dim Count As Integer = 1
For X As Integer = 0 To str.Length - 1
Dim c As Char = str(X)
If Not c = " " Then Count += 1 Else Count = 1
If (Count / 2).ToString.Contains(",") Then _
MixedString += c.ToString.ToUpper _
Else MixedString += c.ToString.ToLower
Next
Dim rgx As New System.Text.RegularExpressions.Regex("\s+")
Return rgx.Replace(MixedString, Delimiter)
Case StringCase.DelimitedCase_Mixed_Word_Upper
Dim MixedString As String = Nothing
Dim Count As Integer = 1
For X As Integer = 0 To str.Length - 1
Dim c As Char = str(X)
If Not c = " " Then Count += 1 Else Count = 1
If (Count / 2).ToString.Contains(",") Then _
MixedString += c.ToString.ToLower _
Else MixedString += c.ToString.ToUpper
Next
Dim rgx As New System.Text.RegularExpressions.Regex("\s+")
Return rgx.Replace(MixedString, Delimiter)
Case Else
Return Nothing
End Select
End Function
#End Region