【java学习记录】spilt小结

package String;

import java.util.Arrays;

/*
 * String spilit(String str)
 * 将字符串以str标识符拆分成若干个字符串
 * 
 * /
 */
public class DemoSpilt {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str1 = "1,2,3,4,5";
		String[] arr1 = str1.split(",");
		System.out.println(arr1.length);
		System.out.println(Arrays.toString(arr1));
		
		//趟若中间有若干个“,”又会是怎样的结果
		/*
		 * 有输出结果我们可以看到中【1,2,3,,,4,5,65,67,78,8,9,9,0】
		 * 上面的连续3个都好表明中间是空的字符串
		 */
		String str2 =  "1,2,3,,,4,5";
		String[] arr2 = str2.split(",");
		System.out.println(arr2.length);
		System.out.println(Arrays.toString(arr2));
		
		/*
		 * 趟若","出现在前面会怎么样?
		 * 同样和出现在中间的效果一样
		 */
		String str3 =  ",,,1,2,3,4,5";
		String[] arr3 = str3.split(",");
		System.out.println(arr3.length);
		System.out.println(Arrays.toString(arr3));
		
		/*
		 * 但是如果出现在结尾呢?
		 * 从输出结果我们可以看到当出现在结尾时并没有上述的想象,
		 * 也就是说当后面再没有字符的时候就相当于无效的字符
		 */
		String str4 =  "1,2,3,4,5,,,";
		String[] arr4 = str4.split(",");
		System.out.println(arr4.length);
		System.out.println(Arrays.toString(arr4));
	}
}

你可能感兴趣的:(Java学习笔记)