Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

How to Toggle String Case in .NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
23 Feb 2011CPOL 4.5K   2   1
Hi,Don`t know how to test its speed, but this does the trick and it also handles unicode. My aproach was to test for casing the least ammount of times as possible, I didn`t figure out how to group accented letters, for those it does it one at a time, so there is still room for improvement....
Hi,
Don`t know how to test its speed, but this does the trick and it also handles unicode. My aproach was to test for casing the least ammount of times as possible, I didn`t figure out how to group accented letters, for those it does it one at a time, so there is still room for improvement. Also, I'm using a magic string for the pattern, this should be removed for maybe a constant or something else.
C#
public static string ChangeCasing(string value)
        {
            StringBuilder sb = new StringBuilder();
            foreach (Match m in Regex.Matches(value, "([.\\d\\W]+)|([a-z]+)|([A-Z]+)|(?:\\P{M}\\p{M}*)"))
                sb.Append(char.IsLower(m.Value[0]) ? m.Value.ToUpper() : m.Value.ToLower());
            return sb.ToString();
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Mexico Mexico
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
GeneralReviewed the pattern, this way its correctly grouping Pin
Rogenator23-Feb-11 15:10
Rogenator23-Feb-11 15:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.