The reverse case of all the alphabetic characters in a C # string

What is the easiest way to change case of all alphabetic characters in a C # string? For example, "aBc1 $;" should become "AbC1 $;"; I could easily write a method that does this, but I hope there is a library call that I don't know about that will make this easier. I would also like to avoid having a list of all known alphabetical characters and comparing each character with what is on the list. Perhaps this can be done with regular expressions, but I don't know them very well. Thanks.

Thanks for the help. I created a line extension method for this, which is mostly inspired by Anthony Pegram's solution, but without LINQ. I think this provides a good balance between readability and performance. Here is what I came up with.

public static string SwapCase(this string source) { char[] caseSwappedChars = new char[source.Length]; for(int i = 0; i < caseSwappedChars.Length; i++) { char c = source[i]; if(char.IsLetter(c)) { caseSwappedChars[i] = char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c); } else { caseSwappedChars[i] = c; } } return new string(caseSwappedChars); } 
+6
c # regex case-sensitive
source share
9 answers

You can do this in a line with LINQ. One method:

 string input = "aBc1$"; string reversedCase = new string( input.Select(c => char.IsLetter(c) ? (char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c)) : c).ToArray()); 
+17
source share

If you do not need internationalization:

 string input = " aBc1$@ [\\]^_{|{~"; Encoding enc = new System.Text.ASCIIEncoding(); byte[] b = enc.GetBytes(input); for (int i = input.Length - 1; i >= 0; i -= 1) { if ((b[i] & 0xdf) >= 65 && (b[i] & 0xdf) <= 90) { //check if alpha b[i] ^= 0x20; // then XOR the correct bit to change case } } Console.WriteLine(input); Console.WriteLine(enc.GetString(b)); 

If, on the other hand, you care about internationalization, you need to pass CultureInfo.InvariantCulture in the functions ToUpper () and ToLower () ...

+5
source share

You can do it in the old school if you don't know LINQ.

 static string InvertCasing(string s) { char[] c = s.ToCharArray(); char[] cUpper = s.ToUpper().ToCharArray(); char[] cLower = s.ToLower().ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == cUpper[i]) { c[i] = cLower[i]; } else { c[i] = cUpper[i]; } } return new string(c); } 
+1
source share

Here's a regex approach:

 string input = "aBcdefGhI123jKLMo$"; string result = Regex.Replace(input, "[a-zA-Z]", m => Char.IsUpper(m.Value[0]) ? Char.ToLower(m.Value[0]).ToString() : Char.ToUpper(m.Value[0]).ToString()); Console.WriteLine("Original: " + input); Console.WriteLine("Modified: " + result); 

You can use Char.Parse(m.Value) as an alternative to m.Value[0] . Also, remember to use the ToUpperInvariant and ToLowerInvariant . See this question for more information: In C #, what is the difference between ToUpper () and ToUpperInvariant ()?

+1
source share
  char[] carr = str.ToCharArray(); for (int i = 0; i < carr.Length; i++) { if (char.IsLetter(carr[i])) { carr[i] = char.IsUpper(carr[i]) ? char.ToLower(carr[i]) : char.ToUpper(carr[i]); } } str = new string(carr); 
0
source share

I was asked a similar question yesterday, and my answer is:

 public static partial class StringExtensions { public static String InvertCase(this String t) { Func<char, String> selector= c => (char.IsUpper(c)?char.ToLower(c):char.ToUpper(c)).ToString(); return t.Select(selector).Aggregate(String.Concat); } } 

You can easily change the method signature to add a parameter of type CultureInfo and use it with methods of type char.ToUpper to require globalization.

0
source share

A little faster than some of the other methods listed here, and that's good, because it uses Char arithmetic!

  var line = "someStringToSwapCase"; var charArr = new char[line.Length]; for (int i = 0; i < line.Length; i++) { if (line[i] >= 65 && line[i] <= 90) { charArr[i] = (char)(line[i] + 32); } else if (line[i] >= 97 && line[i] <= 122) { charArr[i] = (char)(line[i] - 32); } else { charArr[i] = line[i]; } } var res = new String(charArr); 
0
source share

I made an extension method for strings that does just that!

 public static class InvertStringExtension { public static string Invert(this string s) { char[] chars = s.ToCharArray(); for (int i = 0; i < chars.Length; i++) chars[i] = chars[i].Invert(); return new string(chars); } } public static class InvertCharExtension { public static char Invert(this char c) { if (!char.IsLetter(c)) return c; return char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c); } } 

To use

 var hello = "hELLO wORLD"; var helloInverted = hello.Invert(); // helloInverted == "Hello World" 
0
source share

This will help you more .. because here I am not using the function directly.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Practice { class Program { static void Main(string[] args) { char[] new_str = new char[50]; string str; int ch; Console.Write("Enter String : "); str = Console.ReadLine(); for (int i = 0; i < str.Length; i++) { ch = (int)str[i]; if (ch > 64 && ch < 91) { ch = ch + 32; new_str[i] = Convert.ToChar(ch); } else { ch = ch - 32; new_str[i] = Convert.ToChar(ch); } } Console.Write(new_str); Console.ReadKey(); } } } 

I am sure this will also work for you .. Thank you.

0
source share

All Articles