字符串逆向输出
2016-05-28 14:32
288 查看
1、思路:将字符串转化为字符串数组,然后根据下标反向输出public void reverse() {
String s = "hello";
char[] c;
c = s.toCharArray(); //转化为字符串数组
for (int i = c.length - 1; i >= 0; i--) {
System.out.print(c[i]); //下标逆向输出
}
}
2、用String的那个charAt()方法public void reverse2() {
String s = "hello";
for (int i = s.length()-1; i >= 0; --i) {
System.out.print(s.charAt(i)); //下标逆向输出
}
}
3、调用StringBuffer的reverse()方法public void reverse1() {
StringBuffer s = new StringBuffer("hello");
System.out.println(s.reverse());
}
4、字符串两边互掉位置,重新组合形成新的字符串然后输出
String s = "hello";
char[] c;
c = s.toCharArray(); //转化为字符串数组
for (int i = c.length - 1; i >= 0; i--) {
System.out.print(c[i]); //下标逆向输出
}
}
2、用String的那个charAt()方法public void reverse2() {
String s = "hello";
for (int i = s.length()-1; i >= 0; --i) {
System.out.print(s.charAt(i)); //下标逆向输出
}
}
3、调用StringBuffer的reverse()方法public void reverse1() {
StringBuffer s = new StringBuffer("hello");
System.out.println(s.reverse());
}
4、字符串两边互掉位置,重新组合形成新的字符串然后输出
相关文章推荐
- Etcd操作简单介绍
- ZOJ 3811 dfs&并查集
- mysql MHA高可用架构搭建部署实验
- mprotect()函数 Unix/Linux
- hdu-2829 Lawrence[斜率dp]
- 博客前端
- permulation and review
- 7. Reverse Integer
- 其实我想说,如果你用了eventBus,那么你进来看看吧...
- 对象的序列化
- python-布尔值
- 网格 [Bzoj 3907]
- Oralce decode函数用法示例
- javascript、jQuery操作数组
- Bundler
- 人民币升值
- Sql日期查询-SQL查询今天、昨天、7天内、30天
- 类加载机制
- tomcat配置文件详解
- PhoneGap android开发:通讯录