Click here to Skip to main content
15,887,242 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)
11 Feb 2011CPOL 5.8K   3  
Interesting finding in regards to the Case Toggle Algorithm by Robert (see the following code snippet): protected string ToggleCaseByRobert(string s){ var sb = new StringBuilder(s.Length); foreach (char c in s) sb.Append(char.IsUpper(c) ? char.ToLower(c) :...
Interesting finding in regards to the Case Toggle Algorithm by Robert (see the following code snippet):
C#
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();
}


The functionality will stay the same if the line:
var sb = new StringBuilder(s.Length);

is replaced with simplified version:
var sb = new StringBuilder();

but the original version demonstrates about 5...10% performance boost. Kudos to Robert Rohde for this useful tip!

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 DevOps and Data Engineer holds PhD in Electrical and Computer Engineering, authored 37 inventions and published 100+ technical articles, including those popular at CodeProject totaling 4M+ views. Alex pioneered AI/NLP, Cloud development, .NET/Java technology stacks, advanced SQL extensions, HTML5/CSS3 and other important Web technologies; developed multiple award-winning Web/Win apps submitted to App Innovation Contests (AIC 2012/2013). Currently focused on Microsoft Azure Cloud and GitHub Copilot AI-enabled DevOps.

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

Comments and Discussions

 
-- There are no messages in this forum --