1 22 23 package org.gjt.sp.util; 24 25 import java.util.ArrayList ; 27 import java.util.Collection ; 28 30 36 public class StringList extends ArrayList <String > 37 { 38 39 public StringList() 41 { 42 } 43 44 45 public StringList(Object [] array) 46 { 47 addAll(array); 48 } 50 public void addAll(Object [] array) 52 { 53 for (int i = 0; i < array.length; ++i) 54 { 55 add(array[i].toString()); 56 } 57 } 59 63 public static StringList split(String orig, Object delim) 64 { 65 if ((orig == null) || (orig.length() == 0)) 66 return new StringList(); 67 return new StringList(orig.split(delim.toString())); 68 } 70 74 public String toString() 75 { 76 return join("\n"); 77 } 79 public String [] toArray() { 81 int siz = size(); 82 String [] result = new String [siz]; 83 System.arraycopy(super.toArray(), 0, result, 0, siz); 84 return result; 85 } 86 88 93 public static String join(Collection c, String delim) 94 { 95 StringList sl = new StringList(); 96 for (Object o: c) { 97 String s = o.toString(); 98 sl.add(s); 99 } 100 return sl.join(delim); 101 } 102 103 110 public static String join(Object [] arr, String delim) { 111 StringList sl = new StringList(); 112 sl.addAll(arr); 113 return sl.join(delim); 114 } 115 116 117 122 public String join(String delim) 123 { 124 int s = size(); 125 if (s < 1) 126 return ""; 127 if (s == 1) 128 return get(0).toString(); 129 else 130 { 131 StringBuffer retval = new StringBuffer (); 132 retval.append(get(0)); 133 for (int i = 1; i < s; ++i) 134 retval.append(delim + get(i)); 135 return retval.toString(); 136 } 137 138 } 140 public static void main(String args[]) 142 { 143 String teststr = "a,b,c,d,e,f"; 144 StringList sl = StringList.split(teststr, ","); 145 String joinstr = sl.join(","); 146 System.out.println("Test Passed"); 148 149 } private static final long serialVersionUID = -6408080298368668262L; 151 } | Popular Tags |