Case-insensitive Conversions

Source: Internet
Author: User

Suppose there is a string "BaSiC" that needs to be converted to a case, how should we convert it?
 

The simplest method is to call an api:


String str = "BaSiC ";


String strUpper = str. ToUpper ();


String strLower = str. ToLower ();


Method 2: Call the Char. ToUpper () and Char. ToLower () methods and call the api


Method 3: Use the for loop to determine whether the statement is in upper case or in lower case ..


First, observe the similarities and differences between uppercase and lowercase letters:

 

Print the above Code as follows. If you are interested, try to print a table like this:

 

Console. writeLine ("{0,-3} | {1,-6} | {2,-8} | {3,-3} | {4, -6} | {5,-8 }",

 


"Lowercase", "ascil", "binary", "uppercase", "ascil", "binary ");

 

 

 

 

IEnumerable <char> chars = Enumerable. Range ('A', 'z'-'A' + 1). Select (I => (char) I );

 


Foreach (char c in chars)

 


{

 


Char upperC = char. ToUpper (c );

 


Console. writeLine ("{0,-5} | {1,-6} | {2,-10} | {3,-5} | {4, -6} | {5,-10 }",

 


C, (int) c, Convert. ToString (c, 2 ),

 


UpperC, (int) upperC, Convert. ToString (upperC, 2 ));

 


}

 


 

From the above, we can easily see that 'A' is 32 more than the upper-case 'A' ascil. From this point, we can get the following code:

Private static char [] GetUpperChars (string str)

 


{

 


Char [] chars = str. ToCharArray ();

 

 

 

 

For (int I = 0; I <chars. Length; I ++)

 


{

 


If (char. IsLower (chars [I])

 


{

 


Chars [I] = (char) (chars [I]-32 );

 


}

 


}

 

 

 

 

Return chars;

 


}


This Code uses the for loop and adds judgment. If it is in lower case, the value is subtracted from 32, and the call is relatively simple:

 


String strUpper2 = new string (GetUpperChars (str ));

 

The only drawback of this Code is the IsLower judgment. Can I modify chars To Make It uppercase if no judgment is made?

 

If we solve this problem from the perspective of Ascii, we must use case-sensitive judgment. I saw this sentence in the assembly language book:

 

If a solution to a problem leads us to a conflict, it is likely that we have a problem at the starting point of consideration, or the rules we used at first are not suitable.
 


This passage means that we should observe it from other perspectives rather than from the Ascii perspective.

 


If we don't look at it from the Ascii perspective, where can we look at it?

 


We can observe it from the perspective of binary.

 

Or this figure, but our observation angle is changed to a binary system.

 

 

 

 

The binary system of a: 1100001, the binary system of B is 1100010 ,..

 


The binary system of A: 1000001, the binary system of B is 1000010 ,..


We can know that the 5th bits of a are 1, while the 5th bits of A are 0. (from the right side to the left side, start with 0, and the number of the next bits is like this)

 


B's 5th bits are 1, while B's 5th bits are 0,

 


..

 


Therefore, to convert a string to uppercase, you only need to change all the 5th characters of the string to 1.

 


But how can we change the 5th-bit character to 1?

 


The answer is to use the And operation.

 


First, a is a 7-character, so it is only 7 characters, because the bit is expensive when Ascii is set, 8 bits are wasted, and 6 bits are not enough, therefore, the ascii code is 7 bits.

 


A Is 1100001, and we can see that a total of 7 digits.

 


It can perform And with 0101-1111 or 1101-1111.

 

In C #, And is &

 

So you may change the function:

 


For (int I = 0; I <chars. Length; I ++)

 


{

 


Chars [I] = (char) (chars [I] & 11011111 );

 


}
 

 

Stop it first. Do you think the above statements are correct?

 

Then run the command, and the result shows:

 

Why?

 


This is because & in C # uses a decimal number by default, so 11011111,

 


It will become 11,011,111

 


So how can we change 11011111 of the binary system to 10?

 


Int value = Convert. ToInt32 ("11011111", 2 );

 


The value of value is 223, so the code is changed:

 

 

 

For (int I = 0; I <chars. Length; I ++)

 


{

 


Chars [I] = (char) (chars [I] & 223 );

 


}

 

Run the command to get the correct result:

 


Or use hexadecimal notation: 1101-1111. The hexadecimal notation is:

 


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

 


0 1 2 3 4 5 6 7 8 9 A B C D E F

 


1101 = 2 ^ 3 + 2 ^ 2 + 1 = 8 + 4 + 1 = 13 = D

 


1111 = 2 ^ 3 + 2 ^ 2 + 2 ^ 1 + 2 ^ 0 = 8 + 4 + 2 + 1 = 15 = F

 


Therefore, you can modify the above Code:

 


Chars [I] = (char) (chars [I] & 0xdf );

 

 

Similarly, because only 5th bits are different, 7th bits are ignored. Therefore, you can obtain the correct answer when performing the and operation with 0101-1111:

 


The hexadecimal value of 0101-1111 is 0x5f. Therefore, the code can be changed:

 


Chars [I] = (char) (chars [I] & 0x5f );

Author: LoveJenny
 

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.