public static String gsub(String olds,String news,String text){
  if (olds == null || olds.length() == 0) {
    return text;
  }
  if (text == null) {
    return null;
  }
  int oldsIndex=text.indexOf(olds);
  if (oldsIndex == -1) {
    return text;
  }
  StringBuilder buf=new StringBuilder(text.length());
  int prevIndex=0;
  while (oldsIndex >= 0) {
    buf.append(text.substring(prevIndex,oldsIndex));
    buf.append(news);
    prevIndex=oldsIndex + olds.length();
    oldsIndex=text.indexOf(olds,prevIndex);
  }
  buf.append(text.substring(prevIndex));
  return buf.toString();
}

public static String gsub ( String olds , String news , String text ) { if ( olds == null || olds . length ( ) == 0 ) { return text ; } if ( text == null ) { return null ; } int oldsIndex = text . indexOf ( olds ) ; if ( oldsIndex == - 1 ) { return text ; } StringBuilder buf = new StringBuilder ( text . length ( ) ) ; int prevIndex = 0 ; while ( oldsIndex >= 0 ) { buf . append ( text . substring ( prevIndex , oldsIndex ) ) ; buf . append ( news ) ; prevIndex = oldsIndex + olds . length ( ) ; oldsIndex = text . indexOf ( olds , prevIndex ) ; } buf . append ( text . substring ( prevIndex ) ) ; return buf . toString ( ) ; }
substitute string " old " by string " new " in string " text " everywhere . this is static util function that i could not place anywhere more appropriate . the name of this function is from the good - old awk time .
