Determining whether a random string is equal is a common technique in programming. In the C ++ era, we can convert every four bytes of a string into an int object, the Int object compares four characters at a time to achieve relatively efficient string comparison. So can this idea be implemented in C? The answer is yes.
To use the above idea in C #, two problems must be solved. One is to use pointers in C #, and the locations of managed variables pointed by pointers cannot be reallocated by GC. Second, the correspondence between the hosted string and Int or long in the memory.
Many articles have already described how to use pointers in C #. This article will not detail how to enable the unsafe switch, right-click the solution directory, select properties, and select the "allow unsafe code" option in the build dialog box. In this way, you can use the unsafe keyword in C # to mark the areas where pointers can be used. We know that managed variables are randomly allocated, recycled, and adjusted by the system in the memory. Therefore, the pointer to the hosted variable volume may point to the wrong region due to random adjustment of the managed variable. To ensure that the Pointer Points to the same hosted variable from start to end, the fixed statement is provided in C # to complete the task. The pointer marked with fixed points to the Change volume from beginning to end in the area marked with fixed, and the system does not adjust or assign the managed variable. The Code is as follows: String STR = "Hello world! ";
Unsafe
{
Fixed (char * PS = Str)
{
// In this region, PS always points to the managed string Str
}
}
Sizeof can be used to determine that long in C # occupies 8 bytes. In the above Code, if we convert the first 8 bytes of PS into long type, what is the result? We use the following code to test: Long n = 0;
Long nlow = 0, nhigh = 0;
String STR = "Hello world! ";
Unsafe
{
Fixed (char * PS = Str)
{
Char * pstemp = Ps;
N = * (long *) pstemp;
Nlow = N & 0 xFFFF; // obtain the last two bytes
Nhigh = (n> 16) & 0 xFFFF; // obtains the 3th byte.
MessageBox. Show (char) nlow). tostring () + "" + (char) nhigh). tostring ());
Nlow = (n> 32) & 0 xFFFF; // obtain the 5th and 6th bytes.
Nhigh = (n> 48) & 0 xFFFF; // obtains the 7 th and 8 th bytes.
MessageBox. Show (char) nlow). tostring () + "" + (char) nhigh). tostring ());
}
}
It can be seen that the output in the MessageBox above is H, e, l, and l, respectively. They are not the 8 characters that we intuitively think, because in the conversion process, each character is converted from byte to a two-byte short integer short. Therefore, after the string pointer is strongly converted to long, the value of this long variable can be 4 characters. Therefore, comparing the values of two long objects is actually comparing the values of these four characters, in fact, a comparison operation can compare four characters at the same time.
The code for comparing the two strings is as follows: protected bool IsEquals (string str1, string str2)
{
Bool bRet = true;
Int nC1 = 0, nC2 = 0, nLen = 0;
Int I = 0;
If (str1.Length! = Str2.Length) // If the length is not equal, the string is not equal.
{
Return false;
}
// If the length is not a multiple of 4, the complement is used.
NC1 = (str1.Length % 4 )! = 0 )? (4-str1.Length % 4): 0; // calculates the compensation bit.
NC2 = (str2.Length % 4 )! = 0 )? (4-str1.Length % 4): 0; // calculates the compensation bit.
NLen = (nC1> nC2 )? NC1: nC2;
For (I = 0; I {
If (I <nC1)
{
Str1 + = "";
}
If (I <nC2)
{
Str2 + = "";
}
}
Unsafe
{
Fixed (char * psStr1 = str1) fixed (char * psStr2 = str2)
{
Char * psTemp1 = psStr1;
Char * psTemp2 = psStr2;
While (I <str1.Length)
{
If (* (long *) psTemp1! = (* (Long *) psTemp2) // compare four characters at a time
{
BRet = false;
Break;
}
I + = 4;
PsTemp1 + = 4;
PsTemp2 + = 4;
}
}
}
Return bRet;
}