Resolve the different case conversions in c-sharp
- 2020-04-01 23:32:40
- OfStack
Suppose you have a string "BaSiC" that needs to be converted to case, how do you do that?
Simplest way: call API:
String STR = "BaSiC".
String strUpper = STR. The ToUpper ();
String strLower = STR. ToLower ();
The second method: calls char.toupper (), char.tolower (), again calling the API
Third way: use the for loop to determine if it's uppercase or lowercase.
First look at the similarities and differences between uppercase and lowercase letters:
< img style = "border = 0 border - RIGHT - WIDTH: 0 px; DISPLAY: inline. BORDER - TOP - WIDTH: 0 px; BORDER - BOTTOM - WIDTH: 0 px; BORDER - LEFT - WIDTH: 0 px "title = image BORDER = 0 Alt = image SRC =" / / files.jb51.net/file_images/article/201305/2013051314455369.png "WIDTH = 574 height = 448 >
The code above is printed as follows. If you are interested, you can try to print such a form by yourself:
Console. WriteLine (" {0, 3} | | {1, 6} {8} 2, | | {3, 3} {4, and 6} | {5, 8} ",
"Lowercase ", "ascil"," base 2 ", "uppercase ", "ascil"," base 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, and 10} | | {3, 5} {4, and 6} | {5-10} ",
C, c (int), the Convert. ToString (c, 2),
UpperC upperC, (int), the Convert. ToString (upperC, 2));
}
It's easy to see from the top 'a' is 32 more than the ascil of the capital 'a' , from this point you 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 a for loop and adds a judgment. If it's lowercase, subtract 32 from its value.
String strUpper2 = new string(GetUpperChars(STR));
The only disadvantage of this code is that IsLower is used for judgment. Can we change chars to uppercase without judgment?
If we want to solve this problem from the Ascii point of view, then we have to use case judgment. I read this sentence in the assembly language book:
If the solution to a problem leads us into a kind of contradiction, then it is likely that there is a problem in the starting point of our thinking about the problem, or the law we used in the first place is not suitable.
What this means is that maybe we should look at it from other angles than Ascii.
If you don't look at it from an Ascii perspective, where else can you look at it?
You can see it from a base two point of view.
< img style = "border = 0 border - RIGHT - WIDTH: 0 px; DISPLAY: inline. BORDER - TOP - WIDTH: 0 px; BORDER - BOTTOM - WIDTH: 0 px; BORDER - LEFT - WIDTH: 0 px "title = image BORDER = 0 Alt = image SRC =" / / files.jb51.net/file_images/article/201305/2013051314455370.png "WIDTH = 574 height = 448 >
It's the same picture, but we're viewing it in base two.
Base 2 of a: 1100001, base 2 of b: 1100010,..
Base 2 of A: 1000001, base 2 of B: 1000010,..
So we know that the fifth digit of a is 1, and the fifth digit of a is 0, (counting from the right to the left, starting at 0, which digit will follow).
The fifth digit of b is 1, and the fifth digit of b is 0,
.
So if you want to capitalize a string, all you need to do is change the 5th bit of all the characters in the string to 1.
How do you change the fifth bit of a character into a 1?
The answer is to use the And operation.
First of all, a is a 7-bit character, so it's only 7 bits, because when formulating Ascii, bits are expensive,8 bits are wasteful, and 6 bits are not enough, so Ascii code is 7 bits, this point from
A is 1100001 and you can see that there are seven digits.
It can be And with 0101-1111 or with 1101-1111
In C# And is &
So you might change the function to:
For (int I = 0; i.< Chars. Length; I++)
{
Chars [I] = (char)(chars[I] & 11011111);
}
Pause for a moment. Do you think the above is correct?
Then it runs, only to find:
< img style = "border = 0 border - RIGHT - WIDTH: 0 px; DISPLAY: inline. BORDER - TOP - WIDTH: 0 px; BORDER - BOTTOM - WIDTH: 0 px; BORDER - LEFT - WIDTH: 0 px "title = image BORDER = 0 Alt = image SRC =" / / files.jb51.net/file_images/article/201305/2013051314455371.png "WIDTH = 396 height = 71 >
Why is that?
This is because the & in C# operates on decimal digits by default, so 11011111,
It's going to be eleven, one, eleven, one, eleven
So we're going to convert 11011111 from 2 to 10, how do we do that?
Int value = convert.toint32 ("11011111", 2);
The value of value is 223. Therefore, the code is modified as:
For (int I = 0; i. < Chars. Length; I++)
{
Chars [I] = (char)(chars[I] & 223);
}
Running can get the right results:
Or hexadecimal :1101-1111, hexadecimal:
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 ^ 2 ^ 2 + 3 + 1 + 4 = 8 + 1 = 13 = D
1111 = 2 ^ 2 ^ 2 + 3 + 2 + 2 ^ ^ 1 0 = 8 + 4 + 2 + 1 = 15 = F
So you can change the above code to:
Chars [I] = (char)(chars[I] & 0xdf);
Also because only the fifth bit is different, the seventh bit is ignored. Therefore, the correct answer can be obtained by and operation with 0101-1111:
The hexadecimal of 0101-1111 is 0x5f. So the code can be changed to:
Chars [I] = (char)(chars[I] & 0x5f);