1) Trim method
String tt= "AAA";
Tt=tt. Trim () function to go to string end and end spaces
Tt=tt. TrimEnd () Remove string trailing spaces
Tt=tt. TrimStart () Remove string first space
(2) Remove whitespace from characters by ASCII code value
Because the ASCII value of the space is 32, when you remove all the spaces in the string, you simply iterate through all the characters in the string and determine if their ASCII value is 32. The key code to remove all the spaces in the string is as follows:
Charenumerator CEnumerator = textBox1.Text.GetEnumerator (); while (Cenumerator.movenext ()) {bytenewbyte[1= System.Text.Encoding.ASCII.GetBytes (CEnumerator.Current.ToString ()); int Asciicode = (short) (array[0]); if + + = CEnumerator.Current.ToString ();}}
(3) Replace method
Use the Replace method with the string's own: Str.replace ("", "")-------------str is the string to be entered or to be detected.
#的Replace函数, you need to be aware of its parameters:
If its argument is an expression, the system evaluates the expression before preprocessing, and then performs the substitution operation.
For example (Getnamebyid is a function): String strsource="This is an example"; Strsource=strsource.replace ("name", Getnamebyid (1)); Obviously, strsource does not contain "name", so no substitution is performed. However, every time this line of substitution code is executed, Getnamebyid will be called once. If Getnamebyid is a more complex logic, or if the substitution operation is in a loop, it will greatly affect the efficiency of the system. Suggested changes to:if(Strsource.indexof ("name") >-1) {strsource=strsource.replace ("name", Getnamebyid (1)); }
Here are 3 ways to remove only half-width space, not to remove the full-width space.
Remove spaces from a string