1 package org.objectweb.modfact.jmi.helper; 2 3 import java.util.StringTokenizer ; 4 5 8 public class StringHelper { 9 10 17 public static String replaceFirst(String original, String to_replace, String new_value) { 18 int index = original.indexOf(to_replace); 19 if (index != -1) { 20 return original.substring(0, index) + new_value + original.substring(index+to_replace.length()); 21 } else { 22 return original; 23 } 24 } 25 26 33 public static String replaceAll(String original, String to_replace, String new_value) { 34 StringBuffer newString = new StringBuffer (); 35 int indexBegin = 0; 36 int index = original.indexOf(to_replace, indexBegin); 37 while (index != -1) { 38 newString.append(original.substring(indexBegin, index)); 39 newString.append(new_value); 40 indexBegin = index + to_replace.length(); 41 index = original.indexOf(to_replace, indexBegin); 42 } 43 newString.append(original.substring(indexBegin)); 44 return newString.toString(); 45 } 46 47 54 public static boolean matchesValues(String to_parse, String value) { 55 StringTokenizer token = new StringTokenizer (value, "|"); 56 while (token.hasMoreTokens()) { 57 if (to_parse.equals(token.nextToken())) 58 return true; 59 } 60 return false; 61 } 62 63 70 public static boolean endsWithValues(String to_parse, String value) { 71 StringTokenizer token = new StringTokenizer (value, "|"); 72 while (token.hasMoreTokens()) { 73 if (to_parse.endsWith(token.nextToken())) 74 return true; 75 } 76 return false; 77 } 78 79 87 public static String removeAll(String original, String pattern) { 88 boolean negation = false; 90 String toSearch = pattern; 91 if (toSearch.startsWith("[") && toSearch.endsWith("]")) { 92 toSearch = toSearch.substring(1, toSearch.length()-1); 93 if (toSearch.startsWith("^")) { 94 toSearch = toSearch.substring(1); 95 negation = true; 96 } 97 } 98 java.util.List characters = new java.util.ArrayList (); 100 for (int i=0 ; i<toSearch.length() ; i++) { 101 if (i>1 && toSearch.charAt(i-1)=='-') { 102 for (char c=toSearch.charAt(i-2) ; c<=toSearch.charAt(i) ; c++) { 103 characters.add(new Character (c)); 104 } 105 } else if (toSearch.charAt(i) != '-') 106 characters.add(new Character (toSearch.charAt(i))); 107 } 108 StringBuffer newString = new StringBuffer (); 110 for (int i=0 ; i<original.length() ; i++) { 111 char c = original.charAt(i); 112 if (!negation && !characters.contains(new Character (c))) 113 newString.append(c); 114 else if (negation && characters.contains(new Character (c))) 115 newString.append(c); 116 } 117 return newString.toString(); 118 } 119 120 } 121 | Popular Tags |