String.split()问题:中间保留,最后丢弃
String commaSeparatedString = "Foo,,Bar,,Baz,,,";
String[] words = commaSeparatedString.split(",");
//[Foo, , Bar, , Baz]
System.out.println(Arrays.toString(words));
演示
import java.util.Map;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;/*** TODO 在此写上类的相关说明.<br>* @author gqltt<br>* @version 1.0.0 2021年11月11日<br>* @see * @since JDK 1.5.0*/
public class SplitterDemo {/*** @param args*/public static void main(String[] args) {splitOnCharacter();splitOnString();splitPattern();splitTrimEmpty();splitTrimDigit();splitKeepMissing();splitOmitMissing();}/*** 按字符切割.*/static void splitOnCharacter() {char delimiter = '|';String text = "foo|bar|baz";String[] expected = new String[]{"foo","bar","baz"};Iterable<String> values = Splitter.on(delimiter).split(text);int index = 0;for (String value : values) {Assert.assertThat(value, CoreMatchers.is(expected[index++]));}}/*** 按字符串切割.*/static void splitOnString() {String delimiter = "&";String text = "foo&bar&baz";String[] expected = new String[]{"foo","bar","baz"};Iterable<String> values = Splitter.on(delimiter).split(text);int index = 0;for (String value : values) {Assert.assertThat(value, CoreMatchers.is(expected[index++]));}}/*** 按正则表达式切割.*/static void splitPattern() {String pattern = "\\d+";String text = "foo123bar45678baz";String[] expected = new String[]{"foo","bar","baz"};Iterable<String> values = Splitter.onPattern(pattern).split(text);int index = 0;for (String value : values) {Assert.assertThat(value, CoreMatchers.is(expected[index++]));}}/*** 裁剪空串.*/static void splitTrimEmpty() {String delimiter = "&";String text = "foo & bar& baz ";String[] expected = new String[]{"foo","bar","baz"};Iterable<String> values = Splitter.on(delimiter).trimResults().split(text);int index = 0;for (String value : values) {Assert.assertThat(value, CoreMatchers.is(expected[index++]));}}/*** 裁剪数字.*/static void splitTrimDigit(){String delimiter = "&";String text = "1foo&bar2&2baz3";String[] expected = new String[]{"foo","bar","baz"};Iterable<String> values = Splitter.on(delimiter).trimResults(CharMatcher.javaDigit()).split(text);int index = 0;for (String value : values) {Assert.assertThat(value, CoreMatchers.is(expected[index++]));}}/*** 保留空串.*/static void splitKeepMissing(){char delimiter = '|';String text = "foo|bar|||baz";String[] expected = new String[]{"foo","bar","","","baz"};Iterable<String> values = Splitter.on(delimiter).split(text);int index = 0;for (String value : values) {Assert.assertThat(value, CoreMatchers.is(expected[index++]));}}/*** 省略空串.*/static void splitOmitMissing() {char delimiter = '|';String text = "foo|bar|||baz";String[] expected = new String[]{"foo","bar","baz"};Iterable<String> values = Splitter.on(delimiter).omitEmptyStrings().split(text);int index = 0;for (String value : values) {Assert.assertThat(value, CoreMatchers.is(expected[index++]));}}/*** 切割成map.*/static void splitMap() {String startSring = "Washington D.C=Redskins#New York City=Giants#Philadelphia=Eagles#Dallas=Cowboys";Map<String,String> testMap = Maps.newLinkedHashMap();testMap.put("Washington D.C","Redskins");testMap.put("New York City","Giants");testMap.put("Philadelphia","Eagles");testMap.put("Dallas","Cowboys");Splitter.MapSplitter mapSplitter = Splitter.on("#").withKeyValueSeparator("=");Map<String,String> splitMap = mapSplitter.split(startSring);Assert.assertThat(testMap, CoreMatchers.is(splitMap));}
}