Monday, April 17, 2006

 

Case Changer Excluding Literals

This VB.NET function should change the case of strings excluding literals (e.g. like a string within quotes, or double-quotes). You can choose the case and what character should be the literal/text identifier.

Usage:
Dim x as string
x = "I like to 'Eat','EAT','eat', Apples and Bananas."
x = CaseChangeExcludingLiterals(x,True,"'")

x now equals: "I LIKE TO 'Eat','EAT','eat',APPLES AND BANANAS."

Private Function CaseChangerExcludingLiterals(ByVal Source As String, Optional ByValAs Boolean = True, Optional ByVal LiteralIdentifier As Char = Chr(34)) As String ToUpper


Dim SourceReturn As String = ""

Dim InsideQuotes As Boolean = False

For Each SourceCharacter As Char In Source

If SourceCharacter = LiteralIdentifier Then

SourceReturn &= SourceCharacter

If InsideQuotes Then

InsideQuotes = False

Else

InsideQuotes = True

End If

Else

If InsideQuotes Then

SourceReturn &= SourceCharacter

Else

If ToUpper Then

SourceReturn &= Char.ToUpper(SourceCharacter)

Else

SourceReturn &= Char.ToLower(SourceCharacter)

End If

End If

End If

Next

Return SourceReturn

End Function


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?