Java removes the method of full-width and half-width spaces,

Source: Internet
Author: User

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,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.