java拆分字串使用string類的spilt方法,針對某個分隔符來分割一個字串,示例如下:
public class StringSplit { public static void main(String[] args) { String sourceStr = "1,2,3,4,5";//一個字串 String[] sourceStrArray = sourceStr.split(",");//分割出來的字元陣列 for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]); } // 最多分割出3個字串 int maxSplit = 3; sourceStrArray = sourceStr.split(",", maxSplit); for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]); } }}
java拆分字串使用string類的spilt方法,針對某個分隔符來分割一個字串,示例如下:
public class StringSplit { public static void main(String[] args) { String sourceStr = "1,2,3,4,5";//一個字串 String[] sourceStrArray = sourceStr.split(",");//分割出來的字元陣列 for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]); } // 最多分割出3個字串 int maxSplit = 3; sourceStrArray = sourceStr.split(",", maxSplit); for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]); } }}
輸出結果為:2413,4,5