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

How to Toggle String Case in .NET

By , 14 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Test Page for both Algorithms: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx[^]
 
Algorithm by Robert R.:
 
    protected string ToggleCaseByRobert(string s)
    {
        var sb = new StringBuilder(s.Length);
        foreach (char c in s)
            sb.Append(char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c));
        return sb.ToString();
    }
Algorithm by John S.:
    protected string ToggleCaseByJohn(string s)
    {
        StringBuilder mystring = new StringBuilder(s);
        for (int i = 0; i < mystring.Length; i++) {
            char c = mystring[i];
            mystring[i] = Char.IsLower(c) ? Char.ToUpper(c) : Char.ToLower(c);
        }
        return mystring.ToString();
    }

License

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

About the Author

DrABELL
President Infosoft International Inc
United States United States
Dr. A. Bell has 20+ years of Software and Electrical Engineering experience. He is Win/Web veteran, published 200+ articles and authored 37 inventions, currently focused on: Windows 7/8, HTML5, CSS3, jQuery, SQL, .NET, ASP.NET, WPF, C#, Speech Technology and Mobile apps. He's been among recent App submission winners (The Windows 8* & Ultrabook™ App Innovation Contest 2012). Sample pubs:
  1. Edumatter M12: School Math Calculators and Equation Solvers (contest winner)
  2. Engineering Calculator VOLTA-2013 (contest winner)
  3. HTML5 Best Practices: Table formatting via CSS3
  4. Edumatter-M12 for Windows, app overview
  5. Engineering Calculator VOLTA-814D
  6. CoolPhone: phone numbers-to-text converter
  7. SQL generates large data sequence
  8. Aggregate Product function extends SQL
  9. Top-50 Digital Cameras
  10. WebTV Project: Embedded YouTube Player (Goog #1 YouTube API for ASP.NET)
Dr. Bell is personally credited for 10+ Enterprise level projects (Finance/Investment, Engineering, Edu) w/total code base exceeding 250k lines; doing consulting in NYC for 20 yrs.
Follow on   Twitter

Comments and Discussions

 
GeneralI have to question your testing methodology and conclusion a... PinmemberRod Kemp14-Feb-11 1:17 

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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 14 Feb 2011
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid