Click here to Skip to main content
12,634,171 members (26,683 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

1.7K views
2 bookmarked
Posted

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.

You may also be interested in...

Comments and Discussions

 
GeneralReviewed the pattern, this way its correctly grouping Pin
Rogenator23-Feb-11 16:10
memberRogenator23-Feb-11 16: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.

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