java-字符串的反转和替换

String s = "wangjianxin";
StringBuffer sb = new StringBuffer(s);
sb.reverse(); //反转
System.out.println(sb);
sb.replace(0,2,"1");
System.out.println(sb);
out:nixnaijgnaw
out:1xnaijgnaw

replace方法源码

   public synchronized StringBuffer replace(int start, int end, String str) {
        super.replace(start, end, str);
        return this;
    }

你可能感兴趣的:(java-字符串的反转和替换)