Solved

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

Posted on 2006-06-26
8
895 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
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
0
 
LVL 13

Accepted Solution

by:
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);
0
 
LVL 2

Expert Comment

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

Author Comment

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

James.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16983526
     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
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
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16983930
Kinda suprised by the diff in speed.
0
 

Author Comment

by:JAMES
ID: 16983942
Thanks for the test results.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
insert image with its metadata into SQL server Database 3 87
ASP.NET Web API or ASP.NET Core MVC? 3 51
Please explain "Multi-Tenant Services" 5 60
Run time Error 4 34
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

937 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

4 Experts available now in Live!

Get 1:1 Help Now