輸入ASCII值輸出字符串java
一串ASCII碼值,用空格隔開數組
72 101 108 108 111 44 32 119 111 114 108 100 33
Hello, world!
package com.cwstd.test; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String s=sc.nextLine(); String ss[]=s.split(" "); for(String x:ss) { int x1=Integer.parseInt(x); System.out.print((char)x1);//將int型的ASCII值轉化爲char型字符 } } } }
split()方法,有兩個參數split(String regex,int limit),按字符串regex來切割,返回一個字符串數組,數組儲存的是切割後的子串。參數limit爲切割後數組長度,當limit爲1時不進行切割,當limit爲負值或者不賦值就是執行切割到無限次。code
Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String s=sc.nextLine(); String ss[]=s.split(" ",-1);//String ss[]=s.split(" ");不傳參也是同樣的效果 for(String x:ss) { System.out.print(x); } }
1 2 3 4 5 6 7 8 9 10字符串
輸出:
12345678910string
Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String s=sc.nextLine(); String ss[]=s.split(" ",2); System.out.println(ss[0]); System.out.print(ss[1]); }
1 2 3 4 5 6it
輸出:
1
2 3 4 5 6class