** 10.25 split method (split method new String) String class returns a string array, which is separated by the delimiter string configuration. However, this separator is not returned. New Method for the following implementation, the method returns a string array that matches a string of characters separated configuration, also includes a matching character string.
public static String[] split(String s,String regex)
For example, split ( "ab # 12 # 453", "#") returns ab, #, 12, #, and 453 String array configuration, the split ( "a? B? Gf # e", "[? #] ") returns a ,? , B ,? , Gf, # e, and string array configuration.
//package example4; import java.io.*; import java.util.*; import java.util.regex.*; public class ex5 { public static String[] split(String s,String regex) { ArrayList<String> matchList = new ArrayList<String>(); Pattern pat = Pattern.compile(regex); Matcher mat = pat.matcher(s); int index = 0; while(mat.find()) { String match = s.subSequence(index, mat.start()).toString(); matchList.add(match); String match2 = s.subSequence(mat.start(),mat.end()).toString(); matchList.add(match2); index = mat.end(); } String match = s.subSequence(index,s.length()).toString(); matchList.add(match); int resultSize = matchList.size(); while (resultSize > 0 && matchList.get(resultSize-1).equals("")) resultSize--; String[] result = newString [ResultSize]; return matchList.subList (0 , ResultSize) .toArray (Result); } public static void main (String [] args) { String [] ANS = Split ( "ab & # 123 # 453", "#" ); System.out.println ( "pattern string as" ab # 123 # 453 ", matching string is" # "matching result string array is:" ); for ( int I = 0; I <ans.length; I ++ ) { System.out.println (ANS [I]); } String [] ANS2 = Split ( "GF # A B E??", "[#?]" ); System.out.println ( "pattern string is "?? a b gf # e ", the matching string is "[? #]" matching result string array is: " ); for(int i=0;i<ans2.length;i++) { System.out.println(ans2[i]); } } }