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 && i < length; i++){ 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 { if (status != 2){ pos = i; status = 2; } } } return text.substring(0, pos + 1); } }