public class Main{
常見問題,
需要輸入一個數組,但我們不確定大小哦,
那我們可以先輸入一個字符串然後通過split()來變成數組
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String str = s.substring(1,s.length()-1);
String a[] = str.split(",");
int b[] = new int[a.length];
for(int i=0;i<a.length;i++)
b[i] = Integer.parseInt(a[i]);
//註記:
Integer.valueOf("")和Integer.parseInt("")內部實現是一樣的,
它們之間唯一的區別就是Integer.valueOf(“”)返回的是一個Integer對象,
而Integer.parseInt(“”)返回的是一個基本類型的int。
}
}
字符串常用到的函數:
split(String regex) 拆分函數 根據給定的正則表達式的匹配來拆分此字符串
replace() 函數 字符串替換函數
如:String s = "aaabbb"
System.out.print(s.replace("bbb","ccc")); //輸出是 aaaccc
indexof() 返回指定字符在此字符串中第一次出現處的索引
如果此字符串中沒有這樣的字符,則返回 -1。
substring 返回一個新字符串,它是此字符串的一個子字符串 也成爲字符串截取
trim() 去掉起始和結尾的空格
toLowerCase() 轉換爲小寫
toUpperCase() 轉換爲大寫
charAt() 截取一個字符
getChars() 截取多個字符
concat() 連接兩個字符串
例:String s="Welcome to ";
String t=s.concat("AnHui");
toCharArray()
append 添加或插入函數
常見的進制間轉換
時間:2018.12.18