public class Test { /** * 使用递归算法实现字符串trim功能(去掉字符串首尾的空格) * @param str * @return */ public static String trimStr(String str) { if(str==null || "".equals(str)) { return str; } if(str.charAt(0)==' ') { return trimStr(str.substring(1, str.length())); } else if(str.charAt(str.length()-1)==' ') { return trimStr(str.substring(0, str.length()-1)); } return str; } public static void main(String[] args) { System.out.println(trimStr("")); System.out.println(trimStr("hello ")); System.out.println(trimStr(" world")); System.out.println(trimStr(" hello world ")); System.out.println(trimStr("gu weiqiang")); } }
使用递归算法实现字符串trim功能(去掉字符串首尾的空格)
猜你喜欢
转载自guwq2014.iteye.com/blog/2422748
今日推荐
周排行