Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

How to Toggle String Case in .NET

, 14 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Hi everybody-There is a question posted by Rod Kemp in regards to performance benchmarks used for both algorithms (see Alternate 4). The sample code snippet, pertinent to the "ToggleCaseByJohn" algorithm is as follows: DateTime dt = DateTime.Now; for (Int64 i = 0; i <...
Hi everybody-
 
There is a question posted by Rod Kemp in regards to performance benchmarks used for both algorithms (see Alternate 4). The sample code snippet, pertinent to the "ToggleCaseByJohn" algorithm is as follows:
 
        DateTime dt = DateTime.Now;
        for (Int64 i = 0; i < 100001; i++)
        {
            TextBox2.Text = ToggleCaseByJohn(TextBox2.Text);
        }
        TimeSpan ts = DateTime.Now - dt;
        double dur = (double)ts.Milliseconds / 100;
        Label3.Text = dur.ToString() + " us";
 
This technique does not provide the "academic precision' of testing, but rather gives just a first order approximation with adequate accuracy pertinent to this task. Sure thing, more sophisticated benchmarks procedures could be constructed if necessary, but this one could do the job as specified (for example, instead of using the function call the body of the algorithm could be encapsulated within the code snippet shown above). Also, note that the number of iteration is selected as 100001: it should be odd number in order to see the string visually "toggled", because the even number will return the string to the original state.
 
Best regards,
Alexander Bell
 
PS. Link to the Test Page for both Algorithms:
http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx[^]
 
PPS. On a separate note: suggested Alternate 7 is JUST WRONG because suggested "simplified" version:
char.IsUpper(c) ? char.ToLower(c) : c
is not the equivalent of original:
char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c)
As you can see, that "simplified" Alternate 7 version does not convert Lower Case into Upper; it only converts Upper-to-Lower.

License

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

Share

About the Author

DrABELL
President Infosoft International Inc
United States United States
Dr. A. Bell has 20+ years of Software and Electrical Engineering experience. He is Win/Web veteran, published 200+ articles and authored 37 inventions, currently focused on: Windows 7/8, HTML5, CSS3, jQuery, SQL, .NET, ASP.NET, WPF, C#, Speech Technology and Mobile apps. He's been among recent App submission winners (The Windows 8* & Ultrabook™ App Innovation Contest 2012). Sample pubs:
  1. Edumatter M12: School Math Calculators and Equation Solvers (contest winner)
  2. Engineering Calculator VOLTA-2013 (contest winner)
  3. HTML5 Best Practices: Table formatting via CSS3
  4. Edumatter-M12 for Windows, app overview
  5. Engineering Calculator VOLTA-814D
  6. CoolPhone: phone numbers-to-text converter
  7. SQL generates large data sequence
  8. Aggregate Product function extends SQL
  9. Top-50 Digital Cameras
  10. WebTV Project: Embedded YouTube Player (Goog #1 YouTube API for ASP.NET)
Dr. Bell is personally credited for 10+ Enterprise level projects (Finance/Investment, Engineering, Edu) w/total code base exceeding 250k lines; doing consulting in NYC for 20 yrs.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 14 Feb 2011
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid