自己编码字符串反转,用到了replace方法,为什么没有成功?还是打印的原来的字符串?- class Test1
- {
- public static void main(String[] args)
- {
- String str = "abcdefghijklmn";
- System.out.println("源字符串:"+str);
- Fanzhuan f=new Fanzhuan();
-
- System.out.println("反转之后:"+f.fanZ(str));
- }
- }
- class Fanzhuan
- {
- public String fanZ(String s)
- {
- int start=0,end=s.length()-1;
- String s1=null;
- while (start<end)
- {
- s1 = s.replace(s.charAt(start),s.charAt(end));
- start++;
- end--;
- }
- return s1;
- }
- }
复制代码 |
|