public static final String SPECIAL_CHARACTERS_TEMP = "àÀảẢãÃáÁạẠăĂằẰẳẲẵẴắẮặẶâÂầẦẩẨẫẪấẤậẬđĐèÈẻẺẽẼéÉẹẸêÊềỀểỂễỄếẾệỆìÌỉỈĩĨíÍịỊòÒỏỎõÕóÓọỌôÔồỒổỔỗỖốỐộỘơƠờỜởỞỡỠớỚợỢùÙủỦũŨúÚụỤưƯừỪửỬữỮứỨựỰýÝ :+\\<>\"*,!?%$=@#~[]`|^";
public static final String REPLACEMENTS_TEMP = "aAaAaAaAaAaAaAaAaAaAaAaAaAaAaAaAaAdDeEeEeEeEeEeEeEeEeEeEeEiIiIiIiIiIoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOuUuUuUuUuUuUuUuUuUuUuUyY-___\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
public static char[] SPECIAL_CHARACTERS = SPECIAL_CHARACTERS_TEMP
.toCharArray();
public static char[] REPLACEMENTS = REPLACEMENTS_TEMP.toCharArray();
public static String toUrlFriendly(String s) {
int maxLength = Math.min(s.length(), 236);
char[] buffer = new char[maxLength];
int n = 0;
for (int i = 0; i < maxLength; i++) {
char ch = s.charAt(i);
buffer[n] = removeAccent(ch);
// skip not printable characters
if (buffer[n] > 31) {
n++;
}
}
// skip trailing slashes
while (n > 0 && buffer[n - 1] == '/') {
n--;
}
return String.valueOf(buffer, 0, n);
}
public static char removeAccent(char ch) {
int index = SPECIAL_CHARACTERS_TEMP.indexOf(ch);
if (index >= 0) {
ch = REPLACEMENTS[index];
}
return ch;
}
public static String removeAccent(String s) {
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < sb.length(); i++) {
sb.setCharAt(i, removeAccent(sb.charAt(i)));
}
return sb.toString();
}
Không có nhận xét nào:
Đăng nhận xét