1 16 package net.sf.jftp.system; 17 18 import java.io.*; 19 20 21 public class StringUtils 22 { 23 26 public static String cutPath(String s) 27 { 28 int maxlabel = 64; 29 30 if(s.length() > maxlabel) 31 { 32 while(s.indexOf("/") >= 0) 33 { 34 35 s = StringUtils.cutAfter(s, '/'); 36 37 if(s.length() < 16) 38 { 39 StringBuffer sb = new StringBuffer (s); 40 sb.insert(0, ".../"); 41 42 return sb.toString(); 43 } 44 } 45 } 46 47 return s; 48 } 49 50 53 public static String removeStart(String str, String what) 54 { 55 if(str.startsWith(what)) 56 { 57 int x = what.length(); 58 59 return str.substring(x); 60 } 61 else 62 { 63 return str; 64 } 65 } 66 67 70 public static String cutAfter(String str, char c) 71 { 72 for(int i = 0; i < str.length(); i++) 73 { 74 if(str.charAt(i) == c) 75 { 76 return str.substring(i + 1); 78 } 79 } 80 81 return str; 82 } 83 84 88 public static String contains(String [] tmp, String [] str) 89 { 90 for(int i = 0; i < tmp.length; i++) 91 { 92 for(int j = 0; j < str.length; j++) 93 { 94 if(tmp[i].startsWith(str[j])) 95 { 96 return tmp[i]; 97 } 98 } 99 } 100 101 return ""; 102 } 103 104 107 public static boolean strstr(String tmp, char str) 108 { 109 for(int i = 0; i < tmp.length(); i++) 110 { 111 if(tmp.charAt(i) == str) 112 { 113 return true; 114 } 115 } 116 117 return false; 118 } 119 120 123 public static String string(char c) 124 { 125 char[] buf = new char[1]; 126 buf[0] = c; 127 128 return new String (buf); 129 } 130 131 134 public static String getFile(String file) 135 { 136 int x = file.lastIndexOf("/"); 137 138 if(x >= 0) 140 { 141 file = file.substring(x + 1); 142 } 143 144 x = file.lastIndexOf("\\"); 146 147 if(x >= 0) 148 { 149 file = file.substring(x + 1); 150 } 151 152 return file; 157 } 158 159 163 public static String getDir(String tmp) 164 { 165 int x; 166 167 while(true) 168 { 169 x = tmp.indexOf("/"); 170 171 if((x == (tmp.length() - 1)) || (x < 0)) 172 { 173 break; 174 } 175 else 176 { 177 tmp = tmp.substring(x + 1); 178 } 179 } 180 181 while(true) 182 { 183 x = tmp.indexOf("\\"); 184 185 if((x == (tmp.length() - 1)) || (x < 0)) 186 { 187 break; 188 } 189 else 190 { 191 tmp = tmp.substring(x + 1); 192 } 193 } 194 195 return tmp; 196 } 197 198 201 public static boolean isRelative(String file) 202 { 203 if(file.startsWith("/")) 205 { 206 return false; 207 } 208 209 if((file.length() > 2) && (file.charAt(1) == ':')) 211 { 212 return false; 213 } 214 215 return true; 218 } 219 220 223 public static void main(String [] argv) 224 { 225 String a1 = "E:\\programme\\test.html"; 226 String a2 = "programme\\test.html"; 227 String a3 = "test.html"; 228 String a4 = "/programme/test.html"; 229 String a5 = "programme/test.html"; 230 231 System.out.println("getfile: " + getFile(a1) + " - false, " + 232 isRelative(a1)); 233 System.out.println("getfile: " + getFile(a2) + " - true, " + 234 isRelative(a2)); 235 System.out.println("getfile: " + getFile(a3) + " - true, " + 236 isRelative(a3)); 237 System.out.println("getfile: " + getFile(a4) + " - false, " + 238 isRelative(a4)); 239 System.out.println("getfile: " + getFile(a5) + " - true, " + 240 isRelative(a5)); 241 } 242 243 public static String cut(String tmp, String where) 244 { 245 StringBuffer ret = new StringBuffer (); 246 247 for(int i = 0; i < tmp.length(); i++) 248 { 249 if(!string(tmp.charAt(i)).equals(where)) 250 { 251 ret.append(string(tmp.charAt(i))); 252 } 253 254 if(string(tmp.charAt(i)).equals(where)) 255 { 256 return ret.toString(); 257 } 258 } 259 260 return ret.toString(); 261 } 262 } 263 | Popular Tags |