java中字符串 按照字符挨个换行输出-两种方法

  • 方案1:将String 字符串 转化为char 数组 toCharArray
	public static void main(String[] args) {
		String s="aaahssjshs222";
		// 转化为字符数组。
		
		char[] c=s.toCharArray();
		
		for(int i=0;i<=c.length-1;i++)
		{
			System.out.println(c[i]);
		}
		
	}
  • 方案2:利用String 字符串 的方法 .charAt(index)依次输出
public class OutStringChar {
	public static void main(String[] args) throws InterruptedException {
		String s="aaahssjshs222";
		// 转化为字符数组。
		// charAt(index)String的方法,可以选择第n个字符
		//s.length()  注意是要加括号的,跟数组长度不一致。 
		// 如果数组是c 那么数组长度为c.length
		for(int i=0;i<=s.length()-1;i++)
		{
			System.out.println(s.charAt(i));
			// 输出一个停止1秒 
			Thread.sleep(1000);

		}
		
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_43251783/article/details/83583266
今日推荐