package com.itheima;
/**
* 将字符串中进行反转。abcde --> edcba
*
* 思路
* 1.将此功能封装一个方法
* 2.通过for循环,并通过操作字符 串的方法charAt方法来取出每个字符
* 3.设置一个新字符串ss=="";那么将每次取出来的字符加在字符串ss前面,就可以得到相应
* 字符串的反转字符串
* 4.返回反转后的字符串,并在测试方法中进行测试*/
public class Test5 {
public static void main(String[] args) {
String s ="abcdef2556621";
//调用自定义方法将字符串进行反转
String s1=reverse(s);
//在控制台上输出反转后的字符串
System.out.println(s1);
}
//新建一个反转字符串的功能
public static String reverse(String s)
{
int length = s.length();
String ss = "";
for (int i=0; i <length; i++)
{
ss = s.charAt(i) +ss ;
}
return ss;
}
public static void reverse2(String s)
{
StringBuffer ss = new StringBuffer(s);
ss.reverse();
System.out.println(ss);
}
}
原文:http://blog.csdn.net/zl18603543572/article/details/46548851