首页 > 其他 > 详细

字符串逆向输出

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

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

字符串逆向输出

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!