java 模拟trim去掉字符串两端的空格


//模拟trim去掉字符串两端的空格;
public class TrimDemo 
{
public static void main(String[] args) 
{
String str = "  s  ";
int start = 0;
int end = str.length() - 1;

while(start < end && (str.charAt(start) == ' '))
{
start++;
}
while(start < end && (str.charAt(end) == ' '))
{
end--;
}
str = str.substring(start, end + 1);
System.out.println(str);
}
}

你可能感兴趣的:(Java)