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 + ")");}}