Click here to Skip to main content
15,880,543 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.
0.00/5 (No votes)
23 Feb 2011CPOL 8.9K   2   6
I think if you just want to toggle the case of letters in some string, you don't need any special algorithms and sophisticated methods.In fact, no matter what method you select, you must remember that POINTER is the fastest runner.After reading all methods submitted above, I should say that...
I think if you just want to toggle the case of letters in some string, you don't need any special algorithms and sophisticated methods.
In fact, no matter what method you select, you must remember that POINTER is the fastest runner.
After reading all methods submitted above, I should say that the method actually is very simple. Take C# as an example, we can use the following method to toggle the string:
C#
public static unsafe string ToggleString(string s)
{
    fixed (char* p = s)
    {
        for (int i = 0; i < s.Length; i++)
        {
            if (p[i] > 96 && p[i] < 123) p[i] -= (char)32;
            else if (p[i] > 64 && p[i] < 91) p[i] += (char)32;
        }
    }

    return s;
}


Upper case letters are from 65 to 90, and lower case ones are from 97 to 122. So the diff-value between one upper case letter and its lower counterpart is 32!

Let's test it with 1000,0000 loops, run it several times:
C#
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 10000000; i++)
{
	ToggleString("AbCdEfGhIjKlMnOpQrStUvWxYz");
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);


The result is:
2793
2804
2799
2776
...


The average is 2790, about 3 seconds, how about your results ?

License

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


Written By
Software Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralActually, in my browser, Alternate 13 is regular expression,... Pin
Youzelin25-Feb-11 3:22
Youzelin25-Feb-11 3:22 
Actually, in my browser, Alternate 13 is regular expression, and Alternate 10 is using unsafe code.
Alternate 14 is mine. Maybe the codeproject processed it with some difference. Frown | :-(
GeneralRe: Yeah, it's possible that ordinal numbers are different for d... Pin
DrABELL25-Feb-11 12:35
DrABELL25-Feb-11 12:35 
General@Kevinyou- 1. Alternate 13 DOES NOT use the regular express... Pin
DrABELL25-Feb-11 2:30
DrABELL25-Feb-11 2:30 
GeneralAlternate 13 is the best? Do you read my alternate carefully... Pin
Kevinyou24-Feb-11 19:38
Kevinyou24-Feb-11 19:38 
GeneralHow to test? I just find a list of buttons to click, how can... Pin
Kevinyou24-Feb-11 19:27
Kevinyou24-Feb-11 19:27 
GeneralHi Kevinyou, 1. Michael Hansen already has posted the algor... Pin
DrABELL24-Feb-11 2:34
DrABELL24-Feb-11 2:34 

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.