1 21 22 package com.izforge.izpack.util; 23 24 import java.io.File ; 25 import java.util.ArrayList ; 26 27 32 public class StringTool 33 { 34 35 38 41 public StringTool() 42 { 43 super(); 44 } 45 46 49 54 public static void main(String [] args) 55 { 56 System.out.println("Test: string.replace(abc$defg,$de ,null ):" 57 + StringTool.replace("abc$defg", "$de", null, true)); 58 } 59 60 69 public static String replace(String value, String from, String to) 70 { 71 return replace(value, from, to, true); 72 } 73 74 84 public static String replace(String value, String from, String to, boolean aCaseSensitiveFlag) 85 { 86 if ((value == null) || (value.length() == 0) || (from == null) || (from.length() == 0)) { return value; } 87 88 if (to == null) 89 { 90 to = ""; 91 } 92 93 if (!aCaseSensitiveFlag) 94 { 95 from = from.toLowerCase(); 96 } 97 98 String result = value; 99 int lastIndex = 0; 100 int index = value.indexOf(from); 101 102 if (index != -1) 103 { 104 StringBuffer buffer = new StringBuffer (); 105 106 while (index != -1) 107 { 108 buffer.append(value.substring(lastIndex, index)).append(to); 109 lastIndex = index + from.length(); 110 index = value.indexOf(from, lastIndex); 111 } 112 113 buffer.append(value.substring(lastIndex)); 114 result = buffer.toString(); 115 } 116 117 return result; 118 } 119 120 132 public static String normalizePath(String destination, String fileSeparator) 133 { 134 String FILESEP = (fileSeparator == null) ? File.separator : fileSeparator; 135 136 destination = StringTool.replace(destination, "\\", "/"); 137 138 destination = StringTool.replace(destination, "//", "/"); 140 141 destination = StringTool.replace(destination, ":", ";"); 142 destination = StringTool.replace(destination, ";", ":"); 143 144 destination = StringTool.replace(destination, "/", FILESEP); 145 146 if ("\\".equals(FILESEP)) 147 { 148 destination = StringTool.replace(destination, ":", ";"); 149 150 destination = StringTool.replace(destination, ";\\", ":\\"); 153 } 154 155 return (destination); 157 } 158 159 167 public static String normalizePath(String destination) 168 { 169 return (normalizePath(destination, null)); 170 } 171 172 178 public static String stringArrayToSpaceSeparatedString(String [] args) 179 { 180 String result = ""; 181 for (int idx = 0; idx < args.length; idx++) 182 { 183 result += args[idx] + " "; 184 } 185 return result; 186 } 187 188 public static String getPlatformEncoding() 189 { 190 return System.getProperty("file.encoding"); 192 } 193 194 public static String UTF16() 195 { 196 return "UTF-16"; 197 } 198 199 206 public static String stringArrayListToString(ArrayList aStringList) 207 { 208 return stringArrayListToString(aStringList, null); 209 } 210 211 218 public static String stringArrayListToString(ArrayList aStringList, String aLineSeparator) 219 { 220 String LineSeparator = aLineSeparator; 221 if (LineSeparator == null) LineSeparator = System.getProperty("line.separator", "\n"); 222 223 StringBuffer temp = new StringBuffer (); 224 225 for (int idx = 0; idx < aStringList.size(); idx++) 226 { 227 temp.append(aStringList.get(idx)).append(LineSeparator); 228 } 229 230 return temp.toString(); 231 } 232 233 241 public static boolean startsWith(String str, String prefix) 242 { 243 return (str != null) && str.startsWith(prefix); 244 } 245 246 254 public static boolean startsWithIgnoreCase(String str, String prefix) 255 { 256 return (str != null) && (prefix != null) 257 && str.toUpperCase().startsWith(prefix.toUpperCase()); 258 } 259 260 } 261 | Popular Tags |