asp.net C # case conversion detailed

Source: Internet
Author: User

Suppose you have a string "BaSiC" that needs to be converted to case, so how do you convert it?

The simplest way: Invoke the API:

The code is as follows Copy Code

String str = "BaSiC";

string strupper = str. ToUpper ();

string strlower = str. ToLower ();

The second method: Call Char.ToUpper (), Char.ToLower () method, also call API

The third method is to use a for loop to determine if it is uppercase, if it is lowercase ...

First look at the similarities and differences between uppercase and lowercase letters:

Print the above code below, interested can try to print out the form of such a table:
The code is as follows Copy Code

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

" lowercase", "Ascil", "2 ", " uppercase", "Ascil", "2 ");

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));

}

It's easy to see from above that ' a ' is more than the Ascil ' a', from which you can get the following code:
The code is as follows Copy 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 a for loop and adds a judgment, if it's lowercase, then subtracts its value by 32, and the call is simpler:

The code is as follows Copy Code

String strUpper2 = new String (Getupperchars (str));

The only drawback of this code is the islower of the judge, can not judge the situation to modify the chars to make it uppercase?

If we solve this problem from an ASCII perspective, then we have to use the case-by-word judgment, which I read in the assembly language book:

If a solution to a problem causes us to be in a dilemma, it is likely that we have a problem with the starting point of our thinking, or that the laws we used at first are not appropriate.

The meaning of this passage is that perhaps we should look at it from other angles rather than from an ASCII perspective.

If you don't look at it from an ASCII perspective, where else can you look at it?

Can be viewed from a 2-in-system perspective.

Or this picture, but our angle of observation becomes 2.

A's 2:1100001, B's 2 in 1100010, ...

A's 2:1000001, B's 2 in 1000010, ...

You can see that the 5th digit of A is 1, and the 5th digit of A is 0, (from the right to the left, 0 to the first, and the first number in the back).

The 5th digit of b is 1, and the 5th digit of B is 0,

..

So if you want to capitalize a string, you just need to turn all the characters in the string to 1.

And how do you turn the 5th digit of a character into 1?

The answer is to use the and (bitwise operation) operation.

First A is a 7-bit character, so it is 7 bit, because in the development of ASCII time bit more expensive, 8 bit more wasteful, and 6 bit is not enough, so the ASCII code is 7 bit, this point from

A is 1100001 can be seen, a total of 7 bits.

It can be with 0101-1111 or 1101-1111, and

In C # and is &

So you might change the function to:

The code is as follows Copy Code

for (int i = 0; i < chars. Length; i++)

{

Chars[i] = (char) (Chars[i] & 11011111);

}

Do you think the above wording is correct?

And then run, and it turns out:

Why?

This is because the & default action in C # is a decimal number, so 11011111,

It would turn into 11,011,111.

So how do you change from 2 to 11011111 into 10?

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

Value is 223. So the code is modified to:

The code is as follows Copy Code

for (int i = 0; i < chars. Length; i++)

{

Chars[i] = (char) (Chars[i] & 223);

}

Running can get the right results:

or using a 16-in notation: 1101-1111, the 16-in-system 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 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

So the code above can be modified to:

The code is as follows Copy Code

Chars[i] = (char) (Chars[i] & 0XDF);

Also because the 5th digit is different, the 7th digit is ignored. So you can also get the correct answer with the 0101-1111 and operation:

and 0101-1111 of the 16 is 0x5f. So the code can become:

The code is as follows Copy Code
Chars[i] = ( char) (Chars[i] & 0x5f);

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.