/* * [TestRegexSplit.java] * * Summary: Test Regex Pattern.split. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.1 2011-11-13 initial version */ package com.mindprod.example; import java.util.regex.Pattern; import static java.lang.System.*; /** * Test Regex Pattern.split. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2011-11-13 initial version * @since 2011 */ public class TestRegexSplit { /** * main method * * @param args not used */ public static void main( String[] args ) { { // first test, splitting on | final Pattern splitter = Pattern.compile( "\\|" ); final String what = "||abc|def||ghi||"; out.println( "Splitting " + what + " on |" ); final String[] pieces = splitter.split( what ); for ( int i = 0; i < pieces.length; i++ ) { out.println( i + ". [" + pieces[ i ] + "]" ); } // 0. [] // 1. [] // 2. [abc] // 3. [def] // 4. [] // 5. [ghi] out.println( "Lead end embedded empty fields preserved, but not trailing." ); } { // second test, splitting on \r\n final Pattern splitter = Pattern.compile( "\\r\\n" ); final String what = "\r\n\r\nabc\r\ndef\r\n\r\nghi\r\n\r\n"; out.println( "\nSplitting similar newline-embedded string on: " + splitter.toString() ); final String[] pieces = splitter.split( what ); for ( int i = 0; i < pieces.length; i++ ) { out.println( i + ". [" + pieces[ i ] + "]" ); } // 0. [] // 1. [] // 2. [abc] // 3. [def] // 4. [] // 5. [ghi] out.println( "Same pattern holds true for multichar \\r\\n splitter." ); } { // third test, using Pattern.split(,-1) final Pattern splitter = Pattern.compile( "\\|" ); final String what = "||abc|def||ghi||"; out.println( "\nSplitting " + what + " on | with unlimited split(,-1)" ); final String[] pieces = splitter.split( what, -1 ); for ( int i = 0; i < pieces.length; i++ ) { out.println( i + ". [" + pieces[ i ] + "]" ); } // 0. [] // 1. [] // 2. [abc] // 3. [def] // 4. [] // 5. [ghi] // 6. [] // 7. [] out.println( "Lead, embedded and trailing empty fields preserved." ); } { // fourth test , split empty string final Pattern splitter = Pattern.compile( "\\|" ); out.println( "\nSplitting empty string on |" ); final String[] pieces = splitter.split( "" ); for ( int i = 0; i < pieces.length; i++ ) { out.println( i + ". [" + pieces[ i ] + "]" ); } // 0. [] out.println( "Results in single empty new String[1] field, not new String[0]." ); } { // fifth test, split empty string using Pattern.split(,-1) final Pattern splitter = Pattern.compile( "\\|" ); out.println( "\nSplitting empty string on | unlimited split(,-1)" ); final String[] pieces = splitter.split( "", -1 ); for ( int i = 0; i < pieces.length; i++ ) { out.println( i + ". [" + pieces[ i ] + "]" ); } // 0. [] out.println( "Results in single empty new String[1] field, not new String[0]." ); } } }