|
1. Thanks for your note: I typically use "Add comment" because another option is a pop-up, which appears only on mouse hover (not always the case, cuz there is no obvious reason to move the mouse over some others comments
2. I would agree with you on analytical performance assessments of variable algorithms, though some numeric estimates are handy. Tested on pure ASCII test strings (see the test site link) the performance boost by using Ayoola-Bell or Bell-Nedel (fastest ones) algorithms is almost 10:1; tested on Unicode it's about 6:1. In other words, pretty significant performance improvement!
My best, Alex
|
|
|
|
|
Reason for my vote of 1
result = instead of StringBuilder
|
|
|
|
|
Are you going to add the other alternatives to your webinfocentral.com site?
|
|
|
|
|
Got the highest number of alternatives..
I have not seen codeproject tip having so many alternatives ......
|
|
|
|
|
Two fastest Toggle Case Algorithms (Ayoola-Bell and Bell-Nedel), implemented as pure .NET managed solution already have been identified: see Alternate 13. Also, you could try 'em at the test page: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx
|
|
|
|
|
I know, I was just saying that those faster solutions do not provide merely a constant multiple of boosted performance (e.g., 10x better). They provide a varying multiple of boosted performance (e.g., 10*Nx better). Meaning, the longer the string gets, the worse the OP's solution will perform. By the way, I am only notified of your replies if you click "reply" to my comment rather than "add comment".
|
|
|
|
|
Reason for my vote of 5
I am giving my vote of 5 because the idea is original and conceptually good, regardless of implementation, which could vary, providing 2...3 time better performance (see the Alternate 1 and 2)
|
|
|
|
|
Actually, the performance can be increased by a multiple of N (with N being the length of the string). Concatenating strings by repeatedly using the plus operator takes O(N^2) time. The alternates mostly provide O(N) performance (I suspect the LINQ alternate would be O(N^2)).
|
|
|
|
|
[edit] made variable names more C#-ish.
(no vote from me, but I agree with AspDotNetDev)
|
|
|
|
|
Reason for my vote of 5
the way to toggle the string is optimal.. i like it
|
|
|
|
|
you have a strange idea of "optimal"...
|
|
|
|
|
Reason for my vote of 3
Neat idea, poorly implemented. Appending strings like this is a huge performance hit. And your variables are named strangely for C# code.
|
|
|
|
|
|
Hi everybody,
As this post (Toggle Case Algorithms) and the following discussion thread became excessively lengthy and hardly readable, then I would suggest to publish a separate tip, describing the Toggle Case algorithms, which are implemented as "pure" .NET solutions (managed code written in C#) and demonstrate the best performance, namely: Ayoola-Bell and Bell-Nedel (see the Alternate 13).
In regards to the possible "unsafe" solution (using pointers), capable of processing the ASCII/Unicode string, the best "tested" performer so far is presented in Alternate 11 by Michael Hansen (bug corrected by Alex Bell).
Special thanks go to the project originator Manas Bhardwaj, who started this tip, even though his solution was found less efficient that the other listed above
Thanks and regards,
Alex
PS. As a reminder, test page is available online at: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx[^]
|
|
|
|
|
Nice idea DrABell, I'll see if I can get some time and come up with an article.
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Why would you ever toggle the case of a string? Sure, make them all lower or upper, but toggle?
Is this purely academic or did you have some real world reason for such?
|
|
|
|
|
purely academic
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Hi, Manas:
Your solution has a lot of sense (that's why I have rated it 5* .
As FYI: Toggle Case is included in MS Word: refer to that drop-down button captioned Aa, which has several options in regards to string conversion, and notice the last one:
Sentence case
lowercase
UPPERCASE
Capitalize Each Word
tOGGLE cASE
Thanks and regards,
A.Bell
|
|
|
|
|
Andrew-
String operations represent one of the most fundamental IT tasks, thus having effective solution to seemingly "pure academic" problem makes sense. As a possible practical scenario: imagine that someone typed the text line with Caps-Lock "ON"; then this solution could be rather handy to correct the case.
Best regards,
Alex
|
|
|
|
|
|
I agree
|
|
|
|
|
That's a poor example. If a user (incorrectly) has caps lock on, then all the text would require to be lower cased, not toggled.
I can't think of a single scenario where toggling the case of a string characters would be useful. Please enlighten me.
|
|
|
|
|
Hi,
This possible scenario implies that with the Caps Lock on (mistakenly) the User entered string as normal, using "Shift" key to capitalize the letter. Toggle Case algorithms could fix the problem.
Regards,
Alex
|
|
|
|
|
So, here's how contrived your example is.
There's an application which expects user input in some form of mixed case string.
The application detects that the caps lock key is 'mistakenly' on.
It also detects that the user doesn't over-ride the caps lock by way of the shift key to format correct input.
So, it 'toggles' all the characters in the input string.
That's great, you waste your time on that highly probable scenario.
I'll disable the caps lock key programmatically.
|
|
|
|
|
Hi,
Sorry, but it looks like you are making a "strawman" arguments, misrepresenting my scenario and responding to your own scenario, which lead to your own erroneous conclusion (like "waste time") and solution (like "disabling the Caps Lock").
Here what it should be, if you read my posts carefully. The application could be a text editor, like any popular one (e.g. MS Word 2010). Refer to that drop-down button captioned Aa: it has several options in regards to string conversion:
Sentence case
lowercase
UPPERCASE
Capitalize Each Word
tOGGLE cASE
The last one (<b>tOGGLE cASE</b>) is what this algorithm is doing. Obviously, it has some sense (probably - lot of sense) if included in the most popular text editor, like MS Word.
Thanks and regards,
A.Bell
|
|
|
|