1 package org.ashkelon.util; 2 6 7 import java.util.ArrayList ; 8 import java.util.StringTokenizer ; 9 10 import org.apache.oro.text.regex.MalformedPatternException; 11 import org.apache.oro.text.regex.Perl5Compiler; 12 import org.apache.oro.text.regex.Perl5Matcher; 13 import org.apache.oro.text.regex.Perl5Substitution; 14 import org.apache.oro.text.regex.Util; 15 16 17 22 public class StringUtils 23 { 24 public static boolean isBlank(java.lang.String text) 25 { 26 return (text == null || text.trim().equals("")); 27 } 28 29 public static String avoidNull(String text) 30 { 31 return (text==null) ? "" : text; 32 } 33 34 public static String wrap(String text, String wrap) 35 { 36 return wrap + text + wrap; 37 } 38 39 42 public static String join(Object [] text, String concatenator) 43 { 44 if (text.length == 0) { return ""; } 45 String result = (text[0] == null) ? "" : (String ) text[0]; 46 for (int i=1; i<text.length; i++) 47 { 48 result += concatenator + ((text[i] == null) ? "" : (String ) text[i]); 49 } 50 return result; 51 } 52 53 57 public static String join(String text, String concatenator, int numtimes) 58 { 59 if(text==null) { return ""; } 60 if(numtimes <= 0) { return ""; } 61 String result = text; 62 for (int i=1; i<numtimes; i++) 63 { 64 result += concatenator + text; 65 } 66 return result; 67 } 68 69 72 public static String [] split(String text, String delimiter) 73 { 74 ArrayList list = new ArrayList (10); 75 StringTokenizer stk = new StringTokenizer (text, delimiter); 76 while (stk.hasMoreTokens()) 77 { 78 list.add(stk.nextToken().trim()); 79 } 80 String [] results = new String [list.size()]; 81 list.toArray(results); 82 return results; 83 } 84 85 public static void main(String args[]) 86 { 87 String [] list = StringUtils.split("eitan", ","); 88 for (int i=0; i<list.length; i++) 89 { 90 Logger.getInstance().traceln(list[i]); 91 } 92 } 93 94 95 public static boolean getCommandLineOption(String option, String [][] options) 96 { 97 boolean value = false; 98 for (int i = 0; i < options.length; i++) 99 { 100 String [] opt = options[i]; 101 if (opt[0].equals(option)) 102 { 103 value = true; 104 } 105 } 106 return value; 107 } 108 109 public static int getCommandLineOption(String option, String [][] options, int defaultValue) 110 { 111 int value = defaultValue; 112 for (int i = 0; i < options.length; i++) 113 { 114 String [] opt = options[i]; 115 if (opt[0].equals(option)) 116 { 117 try 118 { 119 value = Integer.parseInt(opt[1]); 120 } catch (NumberFormatException ex) 121 { 122 value = defaultValue; 123 } 124 } 125 } 126 return value; 127 } 128 129 public static String getStringCommandLineOption(String option, String [][] options) 130 { 131 for (int i = 0; i < options.length; i++) 132 { 133 String [] opt = options[i]; 134 if (opt[0].equals(option)) 135 { 136 return opt[1]; 137 } 138 } 139 return ""; 140 } 141 142 143 public static String truncate(String text, int length) 144 { 145 if (text.length() > length) 146 { 147 StringBuffer buff = new StringBuffer (text); 148 buff.setLength(length); 149 return buff.toString(); 150 } return text; 152 } 153 154 public static String substitute(String input, String pattern, String replacement) 155 { 156 try 157 { 158 return Util.substitute(new Perl5Matcher(), 159 new Perl5Compiler().compile(pattern), 160 new Perl5Substitution(replacement), 161 input, Util.SUBSTITUTE_ALL); 162 } 163 catch (MalformedPatternException ex) 164 { 165 return input; 166 } 167 } 168 169 170 174 public static String stripNull(String input) 175 { 176 StringBuffer result = new StringBuffer (); 177 char c; 178 for (int i=0; i<input.length(); i++) 179 { 180 c = input.charAt(i); 181 if (c != 0) 182 { 183 result.append(c); 184 } 185 } 186 return result.toString(); 187 } 188 189 } 190 | Popular Tags |