Click here to Skip to main content
15,881,812 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 (1 vote)
14 Feb 2011CPOL 5K   2  

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


Written By
Engineer
United States United States
Dr. Alexander Bell (aka DrABell), a seasoned full-stack Software (Win/Web/Mobile) and Data Engineer holds PhD in Electrical and Computer Engineering, authored 37 inventions and published 100+ technical articles and developed multiple award-winning apps (App Innovation Contests AIC 2012/2013 submissions) Alexander is currently focused on Microsoft Azure Cloud and .NET 6/8 development projects.

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

Comments and Discussions

 
-- There are no messages in this forum --