現在有一個由漢字組成的字符串,想要將他們轉為字符串數組,數組中每個元素為一個漢字。
1.split函數
public static void main(String[] args) throws SQLException { String str1 = "阿迪達斯adidas neo VS JOG男女休閑鞋DB0466EH1696EH1698EH1699"; String[] s = str1.split(""); for(String each : s) { System.out.println(each); } }
2.循環實現
public static void main(String[] args) throws SQLException { String str1 = "阿迪達斯adidas neo VS JOG男女休閑鞋DB0466EH1696EH1698EH1699"; int length = str1.length(); String[] arr2 = new String[length]; for(int i=0; i<length; i++) { arr2[i] = str1.charAt(i) + ""; System.out.println(arr2[i]); } System.out.println(arr2.length); }
運行結果: