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

, 14 Feb 2011 CPOL
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) ...
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)

Share

About the Author

DrABELL
President Infosoft International Inc
United States United States
Dr. A. Bell has 20+ years of Software and Electrical Engineering experience: Win/Web veteran published 300+ articles and authored 37 inventions, currently focused on: Windows 7/8, HTML5, CSS3, jQuery, .NET, ASP.NET, WPF, C#, SQL, 'Big Data', AI, Speech Technology and Mobile apps. He participated in App Innovation Contest (AIC 2102/2013) with several winning submissions. Sample pubs follow:
  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. Engineering Calculator VOLTA-814D
  5. SQL generates large data sequence
  6. Aggregate Product function extends SQL
  7. Watch Your Mouse!
  8. Top-100 Digital Cameras
  9. WebTV Project: Embedded YouTube Player (Goog #1 YouTube API for ASP.NET)
Dr. Bell is personally credited for 10+ Enterprise level projects w/total code base exceeding 250k lines.
Follow on   Twitter

Comments and Discussions

 
GeneralFYI, the algorithm was posted by Robert Rohde. Deeksha just ... PinprotectorAspDotNetDev10-Feb-11 8:57 

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