集册 Java实例教程 通过使用StringBuffer反转字符串

通过使用StringBuffer反转字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

481
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过使用StringBuffer反转字符串
/*
N o w J a v a . c o m 提 供
*/

public class ReverseStringTest {

    public static void main(String[] args) {

        String str = "What's going on?";

        System.out.println(ReverseString.reverseIt(str));

    }

}

class ReverseString {

    public static String reverseIt(String source) {

        int i, len = source.length();

        StringBuffer dest = new StringBuffer(len);


        for (i = (len - 1); i >= 0; i--)

            dest.append(source.charAt(i));

        return dest.toString();/** from n o w j a v a . c o m - 时  代  Java**/

    }

}


展开阅读全文