public class ImitateTrim{
public static void main(String[] args){
String s = " Hello Java ";
sop("s = " + s);
s = imitateTrim(s);
sop("s = " + s);
}
public static String imitateTrim(String str){
int start = 0;
int end = str.length() - 1;
while(start <= end && str.charAt(start) == ' ')
start ++;
while(start <= end && str.charAt(end) == ' ')
end --;
return str.substring(start,end + 1);
}
public static void sop(Object obj){
System.out.println(obj);
}
} |