private static int indexOf(CharSequence source,CharSequence target,int fromIndex){
  int sourceOffset=0, sourceCount=source.length(), targetOffset=0, targetCount=target.length();
  if (fromIndex >= sourceCount) {
    return (targetCount == 0 ? sourceCount : -1);
  }
  if (fromIndex < 0) {
    fromIndex=0;
  }
  if (targetCount == 0) {
    return fromIndex;
  }
  char first=target.charAt(targetOffset);
  int max=sourceOffset + (sourceCount - targetCount);
  for (int i=sourceOffset + fromIndex; i <= max; i++) {
    if (source.charAt(i) != first) {
      while (++i <= max && source.charAt(i) != first)       ;
    }
    if (i <= max) {
      int j=i + 1;
      int end=j + targetCount - 1;
      for (int k=targetOffset + 1; j < end && source.charAt(j) == target.charAt(k); j++, k++)       ;
      if (j == end) {
        return i - sourceOffset;
      }
    }
  }
  return -1;
}

private static int indexOf ( CharSequence source , CharSequence target , int fromIndex ) { int sourceOffset = 0 , sourceCount = source . length ( ) , targetOffset = 0 , targetCount = target . length ( ) ; if ( fromIndex >= sourceCount ) { return ( targetCount == 0 ? sourceCount : - 1 ) ; } if ( fromIndex < 0 ) { fromIndex = 0 ; } if ( targetCount == 0 ) { return fromIndex ; } char first = target . charAt ( targetOffset ) ; int max = sourceOffset + ( sourceCount - targetCount ) ; for ( int i = sourceOffset + fromIndex ; i <= max ; i ++ ) { if ( source . charAt ( i ) != first ) { while ( ++ i <= max && source . charAt ( i ) != first ) ; } if ( i <= max ) { int j = i + 1 ; int end = j + targetCount - 1 ; for ( int k = targetOffset + 1 ; j < end && source . charAt ( j ) == target . charAt ( k ) ; j ++ , k ++ ) ; if ( j == end ) { return i - sourceOffset ; } } } return - 1 ; }
adapted from java . util . string . the source is the character array being searched , and the target is the string being searched for .
