Solved

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

Posted on 2006-06-26
8
891 Views
Last Modified: 2008-01-09
Hi,

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.

James.
0
Comment
Question by:JAMES
8 Comments
 
LVL 10

Expert Comment

by:dkloeck
Comment Utility
i gess you will have to iterate thrtough each char, because there is no method to invert the case of the letters in string
0
 
LVL 13

Accepted Solution

by:
devsolns earned 500 total points
Comment Utility
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);
0
 
LVL 2

Expert Comment

by:Naishal
Comment Utility
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.
0
 

Author Comment

by:JAMES
Comment Utility
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.

James.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Expert Comment

by:pradeepsudharsan
Comment Utility
     string s="ABcdEFgh";
                  char []c=s.ToCharArray();                  
                int c1=0;
                  for(c1=0;c1<c.Length;c1++)
                  {
                        
                        if(char.IsLower(c[c1]))
                              c[c1]=(char)((int)c[c1]-32);
                        else if(char.IsUpper(c[c1]))
                              c[c1]=(char)((int)c[c1]+32);
            
                  }
                  s=new string(c);
                  MessageBox.Show(s);
0
 
LVL 13

Expert Comment

by:devsolns
Comment Utility
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
0
 
LVL 13

Expert Comment

by:devsolns
Comment Utility
Kinda suprised by the diff in speed.
0
 

Author Comment

by:JAMES
Comment Utility
Thanks for the test results.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now