public StringGrabber replaceEnclosedIn ( String startToken , String endToken , String replacement ) { StringCropper cropper = getCropper ( ) ; final List < StrPosition > stringEnclosedInWithDetails = cropper . getStringEnclosedInWithDetails ( sb . toString ( ) , startToken , endToken ) ; final List < Integer > splitList = new ArrayList < Integer > ( ) ; for ( StrPosition sp : stringEnclosedInWithDetails ) { int splitPointFirst = sp . startIndex - 1 ; int splitPointSecond = sp . endIndex ; splitList . add ( splitPointFirst ) ; splitList . add ( splitPointSecond ) ; } final Integer [ ] splitIndexes = splitList . toArray ( new Integer [ ] { } ) ; List < String > splitStringList = cropper . splitByIndex ( sb . toString ( ) , splitIndexes ) ; final StringBuilder tempSb = new StringBuilder ( ) ; final int strNum = splitStringList . size ( ) ; boolean nextIsValue = false ; int countOfReplacement = 0 ; for ( int i = 0 ; i < strNum ; i ++ ) { String strPart = splitStringList . get ( i ) ; if ( nextIsValue ) { // position to add into replacement tempSb . append ( replacement ) ; countOfReplacement ++ ; if ( strPart . startsWith ( endToken ) ) { // is string Blank tempSb . append ( strPart ) ; } } else { tempSb . append ( strPart ) ; } if ( strPart . endsWith ( startToken ) ) { nextIsValue = true ; } else { nextIsValue = false ; } } sb = tempSb ; return StringGrabber . this ; }
replace specified string enclosed in speficied token