65.9K
CodeProject is changing. Read more.
Home

How to Toggle String Case in .Net

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Feb 11, 2011

CPOL
viewsIcon

6048

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):
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!