我换了一种写法,, 也是实现Trim; 用的是迭代;- public class Trim
- {
- static int count =0 ;
- static int count1 = 0 ;
- public static void main(String[] args)
- { String str=" 34dlakj ";
- re(str) ;
- re1(str) ;
- show(str.substring(count,str.length()-count1));
-
- }
- private static void re1(String string) {
- String lString = null ;
- if(string.endsWith(" ")){
- lString = string.substring(0,string.lastIndexOf((char)32)) ;
- if(lString.endsWith(" ")) re1(lString) ;
-
- count1++ ;
- }
-
- }
- private static void re(String string) {
- String kString = null ;
-
- if(string.startsWith(" ")){
- kString= string.substring(1) ;
- if(kString.startsWith(" ")){
-
- re(kString) ;
- }
- count++ ;
- }
- }
- static void show(Object obj)
- {
- System.out.println(obj);
- }
- }
复制代码 |