开发者

Reverse case of all alphabetic characters in C# string

开发者 https://www.devze.com 2023-01-15 07:33 出处:网络
What is the simplest way to reverse the case of all alphabetic characters in a C# string? For example \"aBc1$;\" should become \"AbC1$;\" I cou开发者_开发百科ld easily write a method that does this, b

What is the simplest way to reverse the case of all alphabetic characters in a C# string? For example "aBc1$;" should become "AbC1$;" I cou开发者_开发百科ld easily write a method that does this, but I am hoping there is a library call that I don't know about that would make this easier. I would also like to avoid having a list of all known alphabetic characters and comparing each character to what is in the list. Maybe this can be done with regular expressions, but I don't know them very well. Thanks.

Thanks for the help. I created a string extension method for this that is mostly inspired by Anthony Pegram's solution, but without the LINQ. I think this strikes 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);
}


You could do it 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());


If you don't care about 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 DO care about internationalization, you'll want to pass in CultureInfo.InvariantCulture to your ToUpper() and ToLower() functions...


You could do it 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);
}


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 alternate to m.Value[0]. Also, be mindful of using the ToUpperInvariant and ToLowerInvariant methods instead. For more info see this question: In C# what is the difference between ToUpper() and ToUpperInvariant()?


I made an extension method for strings which does just this!

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"


        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);


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

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 like char.ToUpper for a requirement of globalization.


A little bit faster than some other methods listed here and it is nice because it uses Char arithmetics!

    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);


This will helps you more.. because here i have not use directly function.

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 works for you.. Thank you.


Code below makes only 2 function calls to each letter. Instead of checking if IsLetter, we just apply upper/lowercase if necessary.

    string result="";
    foreach (var item in S)
        {
        if (char.ToLower(item) != item )
            result+= char.ToLower(item);
        else
            result+= char.ToUpper(item);
        }

It would be also possible (tho less readable) to create an extra variable and set it to char.ToLower(item) before the check, exchanging one function call for one extra variable, thie way:

    string result="";
    foreach (var item in S)
        {
        var temp=char.ToLower(item);
        if (temp != item )
            result+= temp;
        else
            result+= char.ToUpper(item);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号