class StringPractice
{
public static void main(String[] args)
{
String str =" dsfdsf gg ";
String s1 =myTrim(str);
sop("["+s1+"]");
}
public static String myTrim(String s)
{
int begin =0;
int end =s.length()-1;
char ch1= s.charAt(begin);
char ch2= s.charAt(end);
while(begin<=end &&ch1==' ')
begin++;
while(begin<=end &&ch2==' ')
end--;
return s.substring(begin,end+1);
}
public static void sop(Object str)
{
System.out.println(str);
}
}
public static String myTrim(String str)
{
int start = 0,end = str.length()-1;
while(start<=end && str.charAt(start)==' ')
start++;
while(start<=end && str.charAt(end)==' ')
end--;
return str.substring(start,end+1);
}
myTrim方法的这两种写法区别在哪?为什么得出的结果差别这么大。。 |
|