Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

How to Toggle String Case in .NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 Feb 2011CPOL 9.7K   3   3
This is an alternative for "How to Toggle String Case in .NET"

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)


Written By
Engineer
United States United States
Dr. Alexander Bell (aka DrABell), a seasoned full-stack Software (Win/Web/Mobile) and Data Engineer holds PhD in Electrical and Computer Engineering, authored 37 inventions and published 100+ technical articles and developed multiple award-winning apps (App Innovation Contests AIC 2012/2013 submissions) Alexander is currently focused on Microsoft Azure Cloud and .NET 6/8 development projects.

  1. HTML5/CSS3 graphic enhancement: buttons, inputs
  2. HTML5 Tables Formatting: Alternate Rows, Color Gradients, Shadows
  3. Azure web app: Engineering Calculator VOLTMATTER
  4. Azure: NYC real-time bus tracking app
  5. Quiz Engine powered by Azure cloud
  6. 'enRoute': Real-time NY City Bus Tracking Web App
  7. Advanced CSS3 Styling of HTML5 SELECT Element
  8. Aggregate Product function extends SQL
  9. YouTube™ API for ASP.NET

Comments and Discussions

 
GeneralI have to question your testing methodology and conclusion a... Pin
Rod Kemp14-Feb-11 1:17
Rod Kemp14-Feb-11 1:17 
GeneralThanks for your note; I have already noticed and corrected t... Pin
DrABELL10-Feb-11 8:52
DrABELL10-Feb-11 8:52 
GeneralFYI, the algorithm was posted by Robert Rohde. Deeksha just ... Pin
AspDotNetDev10-Feb-11 7:57
protectorAspDotNetDev10-Feb-11 7:57 

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.