正则表达式的另一种方法是遍历字符串的字符,然后找出第一个分别最后遇到的字母数字值的开始和结束索引:public static String trim(String input) { int length = input.length(), start = 0, end = length; // iterate from the start // until the first alphanumeric char is encountered while (start < length && notAlphaNumeric(input.charAt(start++))) {} start--; // iterate from the end // until the first alphanumeric char is encountered while (0 < end && notAlphaNumeric(input.charAt(--end))) {} end++; // return the original string if nothing has changed if (start == 0 && end == length) return input; // return an empty string if the indices passed one another if (start >= end) return ""; // else return the substring from start to end return input.substring(start, end);}private static boolean notAlphaNumeric(char c) { return c != ';' && (c < '0' || c > '9') && (c < 'A' || c > 'Z') && (c < 'a' || c > 'z');}我定义为字母数字的值与此正则表达式组匹配:[;0-9a-zA-Z]