1 package net.firstpartners.nounit.utility; 2 3 26 27 31 public class TextUtil { 32 33 47 public static String replace(String string, 48 String oldSubstring, 49 String newSubstring) { 50 String result = string; 51 52 if ((string != null) && (string.length() > 0) 53 && (oldSubstring != null) && (oldSubstring.length() > 0) 54 && (newSubstring != null)) 55 { 56 int pos = string.indexOf(oldSubstring); 57 result = string.substring(0, pos) 58 + newSubstring 59 + string.substring(pos + oldSubstring.length()); 60 } 61 62 return result; 63 } 64 65 66 80 public static String replaceAll( String string, 81 String oldSubstring, 82 String newSubstring) { 83 String result = string; 85 86 87 88 if ( (result != null) 89 && (result.length() > 0) 90 && (result.indexOf(oldSubstring)>-1) 91 && (oldSubstring.length() > 0) 92 && (!oldSubstring.equals(newSubstring))) { 93 94 while (result.indexOf(oldSubstring) > -1) { 95 result = replace(result, oldSubstring, newSubstring); 96 } 97 } 98 99 return result; 100 } 101 102 103 112 public static String find(String fullText, 113 int startIndex, 114 String startMarker, 115 String endMarker) { 116 117 int startPlace=0; 119 int endPlace=0; 120 String foundText=""; 121 122 startPlace = fullText.indexOf(startMarker,startIndex) 124 +startMarker.length(); 125 126 if (startPlace > startIndex) { 128 startIndex = startPlace; 129 } 130 endPlace = fullText.indexOf(endMarker,startIndex); 131 132 try { 134 if ((startPlace>-1) || (endPlace>-1)) { 135 foundText=fullText.substring(startPlace,endPlace); 136 } 137 } catch (java.lang.StringIndexOutOfBoundsException sioobe) { 138 } 140 141 if (startPlace<startIndex) { 143 foundText=""; 144 } 145 146 147 return foundText; 148 } 149 150 151 157 public static String removeAll(String inputString, 158 String removeString) { 159 160 StringBuffer updateString = new StringBuffer (); 162 String tmpString; 163 164 165 for(int a=0; a<inputString.length(); a++ ) { 166 167 tmpString = inputString.substring(a,a+1); 168 if (!tmpString.equals(removeString)) { 169 updateString.append(tmpString); 170 } 171 172 } 173 174 return updateString.toString(); 175 176 } 177 178 184 public static String removeTrailing (String inString, 185 String toRemove) { 186 while (inString.endsWith(toRemove)) { 187 inString=inString.substring(0,inString.length()-toRemove.length()); 188 } 189 190 return inString; 191 192 } 193 194 } | Popular Tags |