Recently, in maintaining the WPF code written by a departing colleague, I stumbled upon two problems with the C # string type, which I recorded here.
1. Use the TRIM function to remove strings such as spaces, newline characters, and so on in a string.
Csret.trim (new char[] {' \ r ', ' \ n ', ' \ t ', '} ');
if (!csret.equals (string. Empty))
Statement1;
Else
Statement2;
The intent of the above code is to remove the string type variable Csret at the beginning and end of the line break, tab, character space, and then determine whether it is empty.
The actual test found that when Csret contains only the four characters, Statement2 is executed, that is, the trim string is not empty. The study found that the trim function was indeed able to remove the specified string from the beginning and end of the string, but instead of manipulating the original string, the action returned a new string after the removal operation was performed. So if you want to change the value inside the Csret to a trim value, you can use the following method:
Csret =
Csret.trim (new char[] {' \ r ', ' \ n ', ' \ t ', '} ');
if (!csret.equals (string. Empty))
Statement1;
Else
Statement2;
2. After using the Remove function, the value of the original string has not changed
Because the original string does not change the same after using the TRIM function above, the Remove function does not change the original string but returns a new string type variable. To modify the value of the original string type variable, simply perform an assignment and reassign the string returned by the Remove function to the original string.
Two problems encountered by C # string type