Implementation of the trim method in the String class of java, stringtrim

Source: Internet
Author: User

Implementation of the trim method in the String class of java, stringtrim

Package stringTest; public class StringDemo4 {public static void main (String [] args) {String str = "abcdefg"; System. out. println ("original string:"); System. out. println ("str = (" + str + ")"); StringDemo4 sd4 = new StringDemo4 (); System. out. println ("string after spaces are removed:"); sd4.sop (sd4.myTrim (str); str. trim ();} String myTrim (String str) {int start = 0; int end = str. length ()-1;/*** Note: start <= end and str. charAt (start) = ''the order of the two conditions cannot be changed. * otherwise, when the entire character is converted to a space, the start value is equal to str. length (), which is greater than str. length ()-1 * condition str. charAt (start) = ''causes an out-of-bounds corner mark exception StringIndexOutOfBoundsException. * & indicates that after the first condition is false, the second condition will not be executed */while (start <= end & str. charAt (start) = '') {start ++;} while (end> = start & str. charAt (end) = '') {end --;} return str. substring (start, end + 1);} // print the String void sop (String str) {System. out. println ("str = (" + str + ")");}}

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.