public static String encodeString ( String value ) { int estimatedSize = 0 ; final int len = value . length ( ) ; // estimate output string size to find out whether encoding is required and avoid reallocations in string builder for ( int i = 0 ; i < len ; ++ i ) { final char ch = value . charAt ( i ) ; if ( ch <= ' ' || ch == ' ' ) { estimatedSize += 3 ; continue ; } ++ estimatedSize ; } if ( value . length ( ) == estimatedSize ) { return value ; // return value as is - it does not contain any special characters } final StringBuilder builder = new StringBuilder ( estimatedSize ) ; for ( int i = 0 ; i < len ; ++ i ) { final char ch = value . charAt ( i ) ; if ( ch <= ' ' ) { builder . append ( "%20" ) ; continue ; } if ( ch == ' ' ) { builder . append ( "%2c" ) ; continue ; } builder . append ( ch ) ; } return builder . toString ( ) ; }
Encodes a value so that it won t contain spaces commas and equal signs .