Click here to Skip to main content
Click here to Skip to main content

How to Toggle String Case in .NET

By , 23 Feb 2011
 
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:
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:
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)

About the Author

Kevinyou
Software Developer
China China
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralActually, in my browser, Alternate 13 is regular expression,...memberYouzelin25 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...memberDrABELL25 Feb '11 - 12:35 
Yeah, it's possible that ordinal numbers are different for different users. That's why I plan to publish a new clean version of this discussion thread; current one is hardly readable already.
General@Kevinyou- 1. Alternate 13 DOES NOT use the regular express...memberDrABELL25 Feb '11 - 2:30 
@Kevinyou-
 
1. Alternate 13 DOES NOT use the regular expression (you probably confused it with Alternate 14). I would agree that Regex is probably the worst solutions among all presented in this comments thread.
2. Alternate 11 is using unsafe code (see char* p = str)
3. Test setting is using about 100,001 iterations: see the sample elapsed time computation posted at Alternate 11 (code snippet started with:
DateTime dt = DateTime.Now;
// using 100001 iterations
for (int j = 0; j < 100001; j++)
4. You could test your algorithm in your own environment using ASCII/Unicode test phrases from that page.
 
Regards, Alex Bell
GeneralAlternate 13 is the best? Do you read my alternate carefully...memberKevinyou24 Feb '11 - 19:38 
Alternate 13 is the best? Do you read my alternate carefully ? Do you noticed I use the FOR loop, and loop count is 1000,0000. If you use the regular expression to do the same thing, you can do a bath before you get the ellapsed time! Actually, REGULAR EXPRESSION is the worst one!
Alternate 11 did not use pointer, he just used array and char. Smile | :)
 
If you decide which is the best by execute one method once a time, you will get the wrong answer.
GeneralHow to test? I just find a list of buttons to click, how can...memberKevinyou24 Feb '11 - 19:27 
How to test? I just find a list of buttons to click, how can I put my code into the website? I've registered and cannot login the website. it always tell me: Your login attempt was not successful. Please try again.
GeneralHi Kevinyou, 1. Michael Hansen already has posted the algor...memberDrABELL24 Feb '11 - 2:34 
Hi Kevinyou,
 
1. Michael Hansen already has posted the algorithm, using pointers (see Alternate 11), which indeed is the fastest, and also, could process the Unicode. But, as a pure .NET managed code solution, Alternate 13 provides the best performance.
2. Did you test your algorithm on any Unicode string? (see samples at the test page: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx)
 
Regards,
Alex

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 Feb 2011
Article Copyright 2011 by Kevinyou
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid