public static void test08(String str) {
String newString="";
for (int i = 0; i < str.length(); i++) {
newString=newString+str.charAt(str.length()-1-i);
}
System.out.println(newString);
}
public static void main(String args[]) {
test08("abcdefgh");//"abcdefgh"为传入的参数。
//System.gc();
//Runtime.getRuntime().gc();
String newString="";
for (int i = 0; i < str.length(); i++) {
newString=newString+str.charAt(str.length()-1-i);
}
System.out.println(newString);
}
public static void main(String args[]) {
test08("abcdefgh");//"abcdefgh"为传入的参数。
//System.gc();
//Runtime.getRuntime().gc();
}
结果是:
hgfedcba
本文介绍了一种使用Java实现字符串反转的方法。通过遍历输入字符串并从尾部开始逐个字符拼接,最终形成反转后的字符串。示例代码展示了如何定义一个接受字符串参数的方法,并输出反转后的结果。
见例解&spm=1001.2101.3001.5002&articleId=8842603&d=1&t=3&u=60dcb0cc0afb4a548ebfff6c086cdcfe)
42

被折叠的 条评论
为什么被折叠?



