Java
Chilkat2 Java
URL Encoding and Decoding
See more Encryption Examples
Demonstrates URL encoding and decoding.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// To URL encoding a string:
String s = "Why a > b?";
StringBuilder sb = new StringBuilder();
sb.append(s);
// URL encode the string.
sb.encode("url", "utf-8");
// Show the URL encoded string:
String sEncoded = sb.getAsString();
System.out.println(sEncoded);
// The result is: Why%20a%20%3E%20b%3F
// If you prefer "+" instead of "%20" for SPACE chars:
int numReplaced = sb.replace("%20", "+");
System.out.println(sb.getAsString());
// Output is: Why+a+%3E+b%3F
// To decode:
sb.decode("url", "utf-8");
System.out.println(sb.getAsString());
// Result is: Why a > b?
}
}