remove spaces in Java1. String.Trim () trim () removes the leading and trailing spaces2. Str.replace (" ",""); Remove all spaces, including the end-to-end, Middle String str="Hell o"; String str2= Str.replaceall (" ",""); System. out. println (str2);3. or ReplaceAll (" +",""); Remove all spaces4. str =. ReplaceAll ("\\s*",""); Can replace most white space characters, not limited to space \s can match space, tab, and other white space characters, any one of the Java remove the full-width space and half-width space method, demand 1: Convert a string into a character array string value="Youzidong"; Char[] val =New Char[Value.length ()]; Value.getchars (0, Value.length (), Val,0);//converting strings to character arraysSystem. out. println (val.length) Requirement 2: Remove all full-width spaces and half-width spaces from the system. out. println (Value.replaceall ("|","") ; demand 3: Remove the half-width space, the full-width space, on either side of the string (call Mytrim (value," ");)StaticString Mytrim (string source, string Totrim) {//Remove the half-width and full-width spaces on either side of the string, or you canStringBuffer SB =NewStringBuffer (source); while(Totrim.indexof (NewCharacter (Sb.charat (0). ToString ())! =-1) {Sb.deletecharat (0); } while(Totrim.indexof (NewCharacter (Sb.charat (Sb.length ()-1) . toString () )!= -1) {Sb.deletecharat (Sb.length ()-1); } returnsb.tostring (); } Complete code: Package com.konglong.test; Public classTrimtest { Public Static voidMain (string[] args) {String value="Youzidong"; Char[] val =New Char[Value.length ()]; Value.getchars (0, Value.length (), Val,0);//converting strings to character arraysSystem. out. println (Val.length); System. out. println (Value.replaceall ("|","")); System. out. println (Mytrim (value," ")); } StaticString Mytrim (string source, string Totrim) {//Remove the half-width and full-width spaces on either side of the string, or you canStringBuffer SB =NewStringBuffer (source); while(Totrim.indexof (NewCharacter (Sb.charat (0). ToString ())! =-1) {Sb.deletecharat (0); } while(Totrim.indexof (NewCharacter (Sb.charat (Sb.length ()-1) . toString () )!= -1) {Sb.deletecharat (Sb.length ()-1); } returnsb.tostring (); }}
Java removes the method of full-width and half-width spaces,