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

Tagged as

Go to top

How to Toggle String Case in .NET

, 17 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Using the .NET profiler, I compared ToggleCaseByRobertToggleCaseByJohnand my new one given below: static string ToggleCaseByCeChode(string s) { char[] chars = s.ToCharArray(); for (int i = 0; i < chars.Length; i++) { ...
Using the .NET profiler, I compared
  • ToggleCaseByRobert
  • ToggleCaseByJohn
  • and my new one given below:
 
        static string ToggleCaseByCeChode(string s)
        {
            char[] chars = s.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                if(chars[i]<65){}
                else if (chars[i] >122) { }
                else if (chars[i] > 90 && chars[i]<97) { }
                else if ((chars[i] & 32) == 32)
                    chars[i] = (char)(chars[i] - 32);
                else
                    chars[i] = (char)(chars[i] | 32);
            }
            return new String(chars);
        }
 
and I'm happy to say that ToggleCaseByCeChode wins by a landslide. Smile | :)
 
Given a 500 character length string and executing each routing 10k times:
 
Elapsed Exlusive Time ( Time spent in this function )       
ToggleCaseByJohn     797.60
ToggleCaseByRobert 715.80
ToggleCaseByCeChode 13.05


Note by Alexander Bell: This algorithm (ToggleCaseByCeChode) indeed provides significant, 5...6 times (!) performance boost against two others, and by far, is the absolute champion in this league of Toggle Case Algorithms!Thumbs Up | :thumbsup: Kudos to CeChode!
 
Also, online Algorithms efficiency comparison is available at: Toggle Case Algorithms[^]

License

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

Share

About the Author

cechode

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 17 Feb 2011
Article Copyright 2011 by cechode
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid