Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

How to Toggle String Case in .NET

, 23 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.
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)

Share

About the Author

Rogenator
Software Developer
Mexico Mexico
Currently working as a freelancer, looking for a steady job.

Comments and Discussions

 
GeneralReviewed the pattern, this way its correctly grouping PinmemberRogenator23-Feb-11 16:10 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 24 Feb 2011
Article Copyright 2011 by Rogenator
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid