Members may post updates or alternatives to this current article in order to show different
approaches or add new features.
1. How to Toggle String Case in .Net
Updated: 9 Feb 2011
StringBuilder mystring = new StringBuilder("AbCd");for (int i = 0; i < mystring.Length; i++){ char c = mystring[i]; mystring[i] = Char.IsLower(c) ? Char.ToUpper(c) : Char.ToLower(c);}
C#, .NET
|
|
|
|
2. How to Toggle String Case in .Net
Updated: 9 Feb 2011
This one should outperform both the other methods for longer strings because of the StringBuilder.string s = "AbCdEfGhI§$%&/()1234567890";var sb = new StringBuilder(s.Length);foreach (char c in s) sb.Append(char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c));s =...
C#, .NET
|
|
|
|
3. How to Toggle String Case in .NET
Updated: 14 Feb 2011
Test Page for both Algorithms: http://webinfocentral.com/resources/toggleCaseAlgorithm.aspx[^]Algorithm by Robert R.: protected string ToggleCaseByRobert(string s) { var sb = new StringBuilder(s.Length); foreach (char c in s) ...
C#, .NET
|
|
|
|
4. How to Toggle String Case in .Net
Updated: 11 Feb 2011
Interesting finding in regards to the Case Toggle Algorithm by Robert (see the following code snippet): protected string ToggleCaseByRobert(string s){ var sb = new StringBuilder(s.Length); foreach (char c in s) sb.Append(char.IsUpper(c) ? char.ToLower(c) :...
C#, .NET
|
|
|
|
5. How to Toggle String Case in .NET
Updated: 14 Feb 2011
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 <...
C#, .NET
|
|
|
|
6. How to Toggle String Case in .NET
Updated: 17 Feb 2011
Couldn't resist: The following is a rewrite of Alternative 9, ToggleCaseHA()This is an unmanaged version which does direct manipulation of the input stringunsafe static string ToggleCaseUnmanaged(string str){ fixed(char* p = str) { for(int i=str.Length-1;i!=-1;--i)...
C#, .NET
|
|
|
|
7. How to Toggle String Case in .NET
Updated: 22 Feb 2011
Following two Toggle Case Algorithms, implemented as "pure" .NET solution (no "unsafe" coding technique, all managed code) demonstrate the best performance, tested against a variety of text strings, containing: ASCII, Unicode, all Low case, all Upper case, long numeric strings (refer to the test...
C#, .NET
|
|
|
|
8. How to Toggle String Case in .NET
Updated: 5 Mar 2011
This code is 27% faster on my machine (.NET 4/AnyCPU on a 64bit i5 processor) for the Unicode test than the Ayoola/Bell algorithm.No unsafe code (in fact was 18% faster than the unsafe version as posted) and works with Unicode.public static string ToggleCase_SimmoTech(string s){ ...
C#, .NET
|
|
|
|
9. How to Toggle String Case in .NET
Updated: 5 Mar 2011
Two more goes and then I'm done. (Wish I could get a job doing this kind of stuff! Wish I could get a job!)This is similar to my previous attempt but Reflector showed how ToLower() and ToUpper() are implemented and we can save some time by caching the CurrentCulture.TextInfo which saves...
C#, .NET
|
|
|
|
10. How to Toggle String Case in .NET
Updated: 7 Dec 2011
For VB.NET users:Public Function ToggleCase(input As String) As String Dim result As String = String.Empty Dim inputArray As Char() = input.ToCharArray() For Each c As Char In inputArray If Char.IsLower(c) Then result += [Char].ToUpper(c) Else result +=...
C#, .NET
|
|
|
|
11. How to Toggle String Case in .NET
Updated: 15 Feb 2011
This does the same. (I'm not sure it's better, but I'm a fan of this style) :)static string toggle(string t){ return (from c in t select new string(Char.IsLower(c) ? Char.ToUpper(c) : Char.ToLower(c), 1)).Aggregate((a, b) => a += b);}much better performance than stringbuilder...
C#, .NET
|
|
|
|
12. How to Toggle String Case in .NET
Updated: 14 Feb 2011
Of the 5 alternatives...somehow the obvious suggestion of dropping the ToUpper() after calling IsUpper() (or ToLower/IsLower) was missed.char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c)to become...char.IsUpper(c) ? char.ToLower(c) : cThis saves half call to a method per character "on...
C#, .NET
|
|
|
|
13. How to Toggle String Case in .NET
Updated: 14 Feb 2011
Shows how to toggle the contents of a string from upper case to lower, or vice versa
C#, .NET
|
|
|
|
14. How to Toggle String Case in .NET
Updated: 17 Feb 2011
Using the .NET profiler, I compared ToggleCaseByRobertToggleCaseByJohnand my new one given below: static string ToggleCaseByCeChode(string s) { char[] chars = s.ToCharArray(); for (int i = 0; i < chars.Length; i++) { ...
C#, .NET
|
|
|
|
15. How to Toggle String Case in .NET
Updated: 8 Mar 2011
Several of the answers here lack fallbacks for Unicode.static string ToggleCaseHA(string str) { char[] chs = str.ToCharArray(); for (int i = chs.Length - 1; i >= 0; i--) { char ch = chs[i]; char foo = (char)(ch & ~0x20); if ((foo >= 0x41 && foo <= 0x5a)...
C#, .NET
|
|
|
|
16. How to Toggle String Case in .NET
Updated: 21 Feb 2011
public static string ToggleCase(string s){ char[] ar = s.ToCharArray(); for(int i = 0; i < ar.Length; ++i) ar[i] = char.IsLower(ar[i]) ? char.ToUpper(ar[i]) : char.ToLower(ar[i]); return new string(ar); }
C#, .NET
|
|
|
|
17. How to Toggle String Case in .NET
Updated: 23 Feb 2011
Hi,Don`t know how to test its speed, but this does the trick and it also handles unicode. My aproach was to test for casing the least ammount of times as possible, I didn`t figure out how to group accented letters, for those it does it one at a time, so there is still room for improvement....
C#, .NET
|
|
|
|
18. How to Toggle String Case in .NET
Updated: 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...
C#, .NET
|
|
|
|
19. How to Toggle String Case in .NET
Updated: 24 Feb 2011
What about this :) string ToggleStringCase(string strToToggle){ StringBuilder sb = new StringBuilder(); foreach(char ch in strToToggle.ToCharArray()) { if((int)ch>=65&&(int)ch<91) { sb.Append((char)((int)ch+32)); } else...
C#, .NET
|
|
|
|
20. How to Toggle String Case in .NET
Updated: 23 Mar 2011
And yet another ...5% slower ... than the fastest to date -> SimmoTech's v2,(an awesome piece of righteously parsimonious code).85% less filling ... the diet version -> 44 vs 256 bytes(.Net v2 and above)private const int AddressBitsPerUnit = 5;private const int BitsPerElement =...
C#, .NET
|
|
|
|
21. How to Toggle String Case in .NET
Updated: 8 Dec 2011
A whole lot less code and a lot easier to do.public static string ToggleCase(this string str){ if (string.IsNullOrEmpty(str)) return str; return string.Join("", (from i in str select (char.IsLetter(i) ? ...
C#, .NET
|
|
|
|
22. How to Toggle String Case in .NET
Updated: 14 Jun 2012
This is an alternative for "How to Toggle String Case in .NET"
C#, .NET
|
|
|
|