C # string case-insensitive mixed conversion,

Source: Internet
Author: User

C # string case-insensitive mixed conversion,

I am a. net newbie. I have learned a lot about Computer Application in college. I have been training for two months in the net direction.

Today, I got a computer question. It would have been quite simple to input one character. If it was in upper case, it would have been converted to lower case. If it was in lower case, it would have been converted to upper case.

 

The difficulty was that we expanded it spontaneously and found that there was no solution to this problem on the Internet.

So I wrote a class for this method and sent it to everyone to share it with you, which is a bit more brainless than the built-in conversion method and can be used for case-sensitive mixed character strings.

 

For example, ASDFasf $ % # % ^ 645765127 aAFSAasdfasd

 

In this case, it is very complicated to convert the upper case to the lower case and convert the lower case to the upper case.

Although it is not very practical, I searched the internet and found that many people are looking for it, but there is no simple method on the Internet.

Therefore, a write conversion can adapt to any mixed case-to-case conversion and no bugs are found in the test.

Although I don't know how many places can be used, since there are so many people on Baidu who want to share it with me

 

---- In fact, it is important to share programming ideas with new people.

 

 1 class Subna 2     { 3         private static char[] x = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M' }; 4         private static char[] o = { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' }; 5  6          7  8         public static string getResult(string zz) 9         {10            11             StringBuilder de = new StringBuilder();12             string zi = zz;13             for (int i = 0; i < zi.Length; i++)14             {15                 int bol = Array.IndexOf(x, zi[i]);16                 if (bol == -1)17                 {18                     int bol2 = Array.IndexOf(o, zi[i]);19                     if (bol2 == -1)20                     {21                         de.Append(zi[i]);22                         continue;23                     }24                     else25                     {26                         for (int da = 0; da < o.Length; da++)27                         {28                             if (o[da] == zi[i])29                             {30                                 de.Append(x[da]);31                                 break;32                             }33                         }34                     }35                 }36                 else37                 {38                     for (int da = 0; da < x.Length; da++)39                     {40                         if (x[da] == zi[i])41                         {42                             de.Append(o[da]);43                             break;44                         }45                     }46                 }47             }48 49             return de.ToString();50 51         }52         53     }
View Code

 

 

This is the class I have encapsulated.

It is a static method. You only need to call and pass parameters.

For example

Console. WriteLine ("\ n enter the string to be converted to uppercase/lowercase :");
String zi = Console. ReadLine ();
String de = zi. ToUpper ();
Console. WriteLine ("\ n converted to {0}", de );

 

One-click call of hybrid conversion is expected to provide the same learning convenience.

 

Remarks to new users: the string type conversion method is not used, so many bugs are avoided in the actual process.

Using basic code to complete complex functions is the same as building blocks, which makes us the primary goal of learning.

Programming is not about how advanced the method is, but about integrating the basic things into the target and returning to practice.

Then I will use the recursion I wrote as an example to share recursion in an easy-to-understand way (and I don't know how to do the false recursion for Recursive question writing when recursion is done)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.