1 package net.javacoding.jspider.core.util; 2 3 4 14 public class StringUtil { 15 16 24 public static String replace ( String string, String pattern, String replacement ) { 25 String replaced = null; 26 27 if (string == null) { 28 replaced = null; 29 } else if (pattern == null || pattern.length() == 0 ) { 30 replaced = string; 31 } else { 32 33 StringBuffer sb = new StringBuffer (); 34 35 int lastIndex = 0; 36 int index = string.indexOf(pattern); 37 while (index >= 0) { 38 sb.append(string.substring(lastIndex, index)); 39 sb.append(replacement); 40 lastIndex = index + pattern.length(); 41 index = string.indexOf(pattern, lastIndex); 42 } 43 sb.append(string.substring(lastIndex)); 44 replaced = sb.toString(); 45 } 46 return replaced; 47 } 48 49 52 public static String replace ( String string, String pattern, String replacement, int start ) { 53 String begin = string.substring(0, start); 54 String end = string.substring(start); 55 return begin + replace(end, pattern, replacement ); 56 } 57 } 58 | Popular Tags |