将字符串转换为可以安全插入URL的内容。
/*来自 N o w J a v a . c o m - 时 代 Java*/ //package com.nowjava; public class Main { /** Converts a string into something you can safely insert into a URL. */ public static String encodeURIcomponent(String s) { StringBuilder o = new StringBuilder(); for (char ch : s.toCharArray()) { if (isUnsafe(ch)) { o.append('%'); o.append(toHex(ch / 16)); o.append(toHex(ch % 16)); } else o.append(ch); } return o.toString(); } /** from * nowjava.com - 时 代 Java **/ private static boolean isUnsafe(char ch) { if (ch > 128 || ch < 0) return true; return " %$&+,/:;=?@<>#%".indexOf(ch) >= 0; } private