码迷,mamicode.com
首页 > 其他好文 > 详细

字符串逆向输出

时间:2014-07-22 09:10:36      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:style   java   color   for   re   c   

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、字符串两边互掉位置,重新组合形成新的字符串然后输出


字符串逆向输出,布布扣,bubuko.com

字符串逆向输出

标签:style   java   color   for   re   c   

原文地址:http://my.oschina.net/u/1414017/blog/289905

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!