code: public static String replace(String source, String pattern,
                                   String replacement, int fromIndex) {
          if (source == null) {
              if (pattern == null) {
                  return replacement;
              }
              else {
                  return source;
              }
          }
  
          int patternLength;
          if (pattern == null || (patternLength = pattern.length()) == 0) {
              return source;
          }
  
          int sourceLength = source.length();
  
          StringBuilder buf;
          if (fromIndex <= 0) {
              fromIndex = 0;
              buf = new StringBuilder(sourceLength);
          }
          else if (fromIndex < sourceLength) {
              buf = new StringBuilder(sourceLength);
              buf.append(source.substring(0, fromIndex));
          }
          else {
              return source;
          }
          
      sourceScan:
          for (int s = fromIndex; s < sourceLength; ) {
              int k = s;
              for (int j=0; j<patternLength; j++, k++) {
                  if (k >= sourceLength || 
                      source.charAt(k) != pattern.charAt(j)) {
  
                      buf.append(source.charAt(s));
                      s++;
                      continue sourceScan;
                  }
              }
  
              buf.append(replacement);
              s = k;
          }
  
          return buf.toString();
      }
docs: Replaces all exact matches of the given pattern in the source string
  with the provided replacement, starting from the given index.
  
  @param source the source string
  @param pattern the simple string pattern to search for
  @param replacement the string to use for replacing matched patterns.
  @param fromIndex index to start the replace
  
  @return the string with any replacements applied.