Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How to Toggle String Case in .NET

, 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...

Alternatives

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

9 Feb 2011
John Simmons / outlaw programmer
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);}
9 Feb 2011
Robert 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 =...
14 Feb 2011
DrABELL
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) ...
11 Feb 2011
DrABELL
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) :...
14 Feb 2011
DrABELL
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 <...
17 Feb 2011
Michael 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)...
22 Feb 2011
DrABELL
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...
5 Mar 2011
SimmoTech
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){ ...
5 Mar 2011
SimmoTech
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...
7 Dec 2011
Jα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 +=...
15 Feb 2011
cechode
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 2011
Manas Bhardwaj
22 alternatives  
Shows how to toggle the contents of a string from upper case to lower, or vice versa
14 Feb 2011
Nathan 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...
14 Feb 2011
TimB99
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...
17 Feb 2011
cechode
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++) { ...
8 Mar 2011
Henry.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)...
21 Feb 2011
nedel
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); }
23 Feb 2011
Rogenator
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....
23 Feb 2011
Kevinyou
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...
23 Mar 2011
MacMaverick
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 =...
8 Dec 2011
Daniel Gidman
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) ? ...
14 Jun 2012
Andreas 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)

Share

About the Author

code in play
Software Developer (Senior)
India India
4+ year experience in Microsoft technologies(C#.Net,ASP.Net, SQL server and Sharepoint).
Follow on   Twitter

| | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 24 Feb 2011
Article Copyright 2011 by code in play
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid