package com.yuji.tdb.utility; import java.util.regex.Pattern; public class StringUtil { private static Pattern ptnAlnum = Pattern.compile("[a-zA-Z0-9]"); private static Pattern ptnAscii = Pattern.compile("\\p{ASCII}"); public static String parseSubstring(String text, int length){ int len = text.length(); int status = -1; int pos = 0; for (int i = 0; i < len + 1 && i < length + 1; i++){ if (i >= len){ pos = i; break; } String ch = text.substring(i, i + 1); if (ptnAlnum.matcher(ch).matches()){ if (status != 0){ pos = i; status = 0; } } else if (ptnAscii.matcher(ch).matches()){ if (status != 1){ pos = i; status = 1; } } else { pos = i; status = 2; } } return text.substring(0, pos); } }