You may know that You can use the string. trim method to remove spaces at the header and tail of the string. Unfortunately, this trim method cannot remove the C # space in the middle of the string.
Static void main ()
{
// Demo1Remove spaces and extract words.
String S = "a B C ";
String [] Word = S. Split (New char [] {''});
Foreach (string temp in word)
Console. writeline (temp );
// Demo2Remove all spaces directly
S = S. Replace ("","");
Console. writeline (s );
// Demo3Remove leading and trailing Spaces
S = "AAA ";
S = S. Trim ();
Console. writeline (s );
}
Another version is as follows:
- StringTEXT ="My test \ nstring \ r \ n is \ t quite long";
- StringTrim = text. Trim ();
The 'trim' string will be:
"My test \ nstring \ r \ n is \ t quite long" (31 characters)
The other method to clear C # spaces is to use the string. Replace method, but you need to call multiple methods to remove individual C # spaces:
- string trim = text. replace ( "" , "" );
- trim = trim. replace ( "\ r" , "" );
- trim = trim. replace ( "\ n" , "" );
- trim = trim. replace ( "\ t" , "" );
The best method here is to use a regular expression. you can use RegEx. replace method, which replaces all matched with the specified character. in this example, the regular expression match "\ s" is used to match any space package containing the C # spaces, TAB characters, line breaks, and newline characters in this string ).
- StringTrim = RegEx. Replace (text ,@"\ S","");
The 'trim' string will be:
- "Myteststringisquitelong"(23 characters)