question: public static String sanitizeStrings ( String str ) { char [ ] c = getChars ( str ) ; int idx = 0 ; String newstr = str ; StringBuilder sb = null ; boolean instring = false ; char stringsep = '\0' ; int lastidx = 0 ; while ( idx < c . length ) { if ( instring && stringsep == c [ idx ] ) { instring = false ; stringsep = '\0' ; } else if ( ! instring ) { if ( '\'' == c [ idx ] ) { instring = true ; stringsep = '\'' ; } else if ( '\"' == c [ idx ] ) { instring = true ; stringsep = '\"' ; } } if ( instring ) { if ( ' ' == c [ idx ] ) { if ( null == sb ) { sb = new StringBuilder ( ) ; } sb . append ( c , lastidx , idx - lastidx ) ; sb . append ( "%20" ) ; lastidx = idx + 1 ; } } idx ++ ; } if ( null != sb ) { if ( lastidx < c . length ) { sb . append ( c , lastidx , c . length - lastidx ) ; } newstr = sb . toString ( ) ; } return newstr ; }
target:  Replace whitespaces in Strings enclosed in single quotes with '%20'