public static String encode ( final String aString , final boolean aIgnoreSlashFlag ) { final CharacterIterator iterator = new StringCharacterIterator ( decode ( aString ) ) ; final StringBuilder builder = new StringBuilder ( ) ; for ( char c = iterator . first ( ) ; c != CharacterIterator . DONE ; c = iterator . next ( ) ) { switch ( c ) { case ' ' : builder . append ( PERCENT ) ; break ; case ' ' : if ( aIgnoreSlashFlag ) { builder . append ( c ) ; } else { builder . append ( "%2F" ) ; } break ; case ' ' : builder . append ( "%3F" ) ; break ; case ' ' : builder . append ( "%23" ) ; break ; case ' ' : builder . append ( "%5B" ) ; break ; case ' ' : builder . append ( "%5D" ) ; break ; case ' ' : builder . append ( "%40" ) ; break ; default : builder . append ( c ) ; break ; } } // Must percent-encode any characters outside the US-ASCII set return URI . create ( builder . toString ( ) ) . toASCIIString ( ) ; }
Percent - encodes supplied string but only after decoding it completely first .