Alternatives
Members may post updates or alternatives to this current article in order to show different
approaches or add new features.
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);}
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 =...
This is an alternative for "How to Toggle String Case in .NET"
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) :...
17 Feb 2011Michael B. Hansen
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)...
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
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){ ...
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...
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 +=...
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...
14 Feb 2011Manas Bhardwaj 22 alternatives
Shows how to toggle the contents of a string from upper case to lower, or vice versa
14 Feb 2011Nathan J Bolstad
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...
What about this?string s = new string(( from c in "ABCdef" select char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c) ).ToArray());Note by Alexander Bell: In regards to the performance, this one (i.e. Alternate 9) is the worst in comparison...
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++) { ...
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)...
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); }
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....
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...
24 Feb 2011pankajupadhyay29
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...
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 =...
14 Jun 2012Andreas Gieriet
This is an alternative for "How to Toggle String Case in .NET"
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.