String split 分隔字符串,多个分隔符、正则表达式学习示例

  • Post author:
  • Post category:其他


import java.util.Arrays;

import org.junit.Test;


/**
 * 通过几个例子学习String split方法
 * 尤其是记住使用多个分隔符的方式
 * @author lzg
 *
 */
public class StringSplitTest {

	/**
	 * 待分隔的字符串全是由分隔符组成
	 */
	@Test	
	public void test1(){		
		String str = ",,,,,,";	
		String [] strArray = str.split(",");		
		
		System.out.printf("length: %d \n",strArray.length);
		//length: 0 
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: []
		
	}
	
	/**
	 * 若有多个分隔符,需放在中括号之中
	 */
	@Test	
	public void test2(){		
		String str = ",,:,;,";	
		String [] strArray = str.split("[,;:]");
		
		System.out.printf("length: %d \n",strArray.length);
		//length: 0 
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: []
		
	}
	
	
	/**
	 * 左侧遇到非分隔字符前,分隔后内容为空也会保留。最右侧分隔后的内容若为空会忽略
	 */
	@Test	
	public void test3(){		
		String str = ",ab,cd,efgh,,,";	
		String [] strArray = str.split(",");	
		
		System.out.printf("length: %d \n",strArray.length); 
		//length: 4 
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: [, ab, cd, efgh]
		
	}
	
	/**
	 * 左侧遇到非分隔字符前,分隔后内容为空也会保留。最右侧分隔后的内容若为空会忽略
	 */
	@Test	
	public void test4(){		
		String str = ",ab,cd:efgh,;,";	
		String [] strArray = str.split("[,;:]");
		
		System.out.printf("length: %d \n",strArray.length); 
		//length: 4
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: [, ab, cd, efgh]
		
	}
	
	
	/**
	 * 若分隔符为正则表达式的特殊字符,则需进行转义
	 */
	@Test	
	public void test5(){		
		String str = "*ab*cd*efgh***";	
		String [] strArray = str.split("\\*");	
		
		System.out.printf("length: %d \n",strArray.length);
		//length: 4 
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: [, ab, cd, efgh]
		
	}
	
	/**
	 * 分隔符为正则表达式的特殊字符,则需进行转义。若中括号本身为分隔符,则需进行转义
	 */
	@Test	
	public void test6(){		
		String str = "*ab+cd^efgh[[[";	
		String [] strArray = str.split("[\\*\\+\\[^]");	
		
		System.out.printf("length: %d \n",strArray.length);
		//length: 4
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: [, ab, cd, efgh]
	}
	
	/**
	 * 多个分隔符之间可以用|分隔,也可以不用。若|为分隔符,则需进行转义
	 */
	@Test	
	public void test7(){		
		String str = "*ab+cd^efgh[[[";	
		String [] strArray = str.split("[\\*|\\+|\\[|^]");	
		
		System.out.printf("length: %d \n",strArray.length); 
		//length: 4
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: [, ab, cd, efgh]
	}
	
	
	/**
	 * 使用标准的正则
	 */
	@Test	
	public void test8(){		
		String str = "*ab+cd^efgh----234---[[.....[";	
		String [] strArray = str.split("\\W+"); //注意正则表达式所代表的含义	
		
		System.out.printf("length: %d \n",strArray.length); 
		//length: 5 
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: [, ab, cd, efgh, 234]
	}
	
	/**
	 * split还有一种带限制的方式,可以指定最多匹配出多少个串
	 */
	@Test	
	public void test9(){		
		String str = "*ab+cd^efgh----234---[[.....[";	
		String [] strArray = str.split("\\W+",3); //限制最多匹配出3个串	
		
		System.out.printf("length: %d \n",strArray.length); 
		//length: 3 
		System.out.printf("content: %s",Arrays.toString(strArray));
		//content: [, ab, cd^efgh----234---[[.....[]
	}
	
}


转载于:https://my.oschina.net/jasonli0102/blog/509349