65.9K
CodeProject is changing. Read more.
Home

How to Toggle String Case in .NET

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Dec 7, 2011

CPOL
viewsIcon

9361

For VB.NET users:Public Function ToggleCase(input As String) As String Dim result As String = String.Empty Dim inputArray As Char() = input.ToCharArray() For Each c As Char In inputArray If Char.IsLower(c) Then result += [Char].ToUpper(c) Else result +=...

For VB.NET users:

Public Function ToggleCase(input As String) As String
	Dim result As String = String.Empty
	Dim inputArray As Char() = input.ToCharArray()
	For Each c As Char In inputArray
		If Char.IsLower(c) Then
			result += [Char].ToUpper(c)
		Else
			result += [Char].ToLower(c)
		End If
	Next
	Return result
End Function

One more:

Dim mystring As New StringBuilder("AbCd")
For i As Integer = 0 To mystring.Length - 1
    Dim c As Char = mystring(i)
    mystring(i) = If([Char].IsLower(c), [Char].ToUpper(c), [Char].ToLower(c))
Next