Invert the case of a mixed case string (can I use regex?)

Posted on 2006-06-26
Last Modified: 2008-01-09

I have a string that contains a mixed bag of upper and lowercase letters.

I need to invert these (upper to lower, lower to upper) so my question is do I have to iterate through each letter and swap it or can I use a smaller, cleaner method (perhaps using a regex expression??).

Many thanks.

Question by:JAMES
LVL 10

Expert Comment

ID: 16983403
i gess you will have to iterate thrtough each char, because there is no method to invert the case of the letters in string
LVL 13

Accepted Solution

devsolns earned 500 total points
ID: 16983434
Put this in a method or what have you,

                string text = "SoMe CaMEL cAse TEXT.";
                char[] textarray = text.ToCharArray();
                for(int i = 0; i < textarray.Length; i++)
                    if((int)textarray[i] >= 65 && (int)textarray[i] <= 90)
                        textarray[i] = (char)((int)textarray[i] + 32);
                    else if ((int)textarray[i] >= 97 && (int)textarray[i] <= 122)
                        textarray[i] = (char)((int)textarray[i] - 32);
                string newText = new string(textarray);

Expert Comment

ID: 16983458
Regular Expression is basically to model the condition or pattern and use that RegEx to check the input and decide if that satisfies the pattern or condition you want it to.

If you dont want to compare string in turn, You can Consider the use of BitMasking going to the Byte Level.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 16983516
OK - accepting there is no regex expression and no intrinsic method Dev's method works great - thank you!

Many thanks for your help dk but I am sure you will understand if i give the points to Dev for a such a detailed answer.


Expert Comment

ID: 16983526
     string s="ABcdEFgh";
                  char []c=s.ToCharArray();                  
                int c1=0;
                        else if(char.IsUpper(c[c1]))
                  s=new string(c);
LVL 13

Expert Comment

ID: 16983922
pradeepsudharsan & everyone,

my implementation of the method runs on average 60% faster than pradeepsudharsan version using the IsLower & IsUpper methods.

Test using 1 million iterations of a couple hundred length string.

-my version
1296 ms
1268 ms
1353 ms

avg:1305 milliseconds

-version using ToUpper & ToLower
2181 ms
2165 ms
2152 ms

avg:2166  milliseconds
LVL 13

Expert Comment

ID: 16983930
Kinda suprised by the diff in speed.

Author Comment

ID: 16983942
Thanks for the test results.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 94
SqlDataBase 7 54
RegEx with optional part 4 42
How can I do Unit Testing with Session ? 1 13
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question