Click here to Skip to main content
15,867,779 members
Articles / Programming Languages / C#

How to Toggle String Case in .NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
8 Dec 2011CPOL 7.3K  
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) ? ...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
9 Feb 2011#realJSOP
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);}
Please Sign up or sign in to vote.
9 Feb 2011Robert Rohde
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 =...
Please Sign up or sign in to vote.
14 Feb 2011DrABELL
This is an alternative for "How to Toggle String Case in .NET"
Please Sign up or sign in to vote.
11 Feb 2011DrABELL
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) :...
Please Sign up or sign in to vote.
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)...
Please Sign up or sign in to vote.
22 Feb 2011DrABELL
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
Please Sign up or sign in to vote.
5 Mar 2011SimmoTech
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){ ...
Please Sign up or sign in to vote.
5 Mar 2011SimmoTech
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...
Please Sign up or sign in to vote.
7 Dec 2011Jαved
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 +=...
Please Sign up or sign in to vote.
15 Feb 2011cechode
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...
Please Sign up or sign in to vote.
14 Feb 2011Manas Bhardwaj 22 alternatives  
Shows how to toggle the contents of a string from upper case to lower, or vice versa
Please Sign up or sign in to vote.
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...
Please Sign up or sign in to vote.
14 Feb 2011TimB99
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...
Please Sign up or sign in to vote.
17 Feb 2011cechode
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++) { ...
Please Sign up or sign in to vote.
8 Mar 2011Henry.Ayoola
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)...
Please Sign up or sign in to vote.
21 Feb 2011nedel
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); }
Please Sign up or sign in to vote.
23 Feb 2011Rogenator
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....
Please Sign up or sign in to vote.
23 Feb 2011Kevinyou
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...
Please Sign up or sign in to vote.
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...
Please Sign up or sign in to vote.
23 Mar 2011MacMaverick
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 =...
Please Sign up or sign in to vote.
14 Jun 2012Andreas Gieriet
This is an alternative for "How to Toggle String Case in .NET"

License

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


Written By
Architect Stackify
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions