因为需要对用户上传的书籍进行章节的连续性校验,比如上次到100章,这次必须从101章开始,规则上从章节名称判断,但是章节名大小写都有可能,格式如下:
样例:第1203章 或 第一千二百零三章
因为时间较紧,简单写了个类做处理,代码如下:
/** * 章节号辅助类 * * @author liyd * */ public class ChapterNumUtil { /** 数字映射 */ private static final Map<Character, Integer> NUM_MAPPING = new HashMap<Character, Integer>(); static { NUM_MAPPING.put('零', 0); NUM_MAPPING.put('一', 1); NUM_MAPPING.put('二', 2); NUM_MAPPING.put('三', 3); NUM_MAPPING.put('四', 4); NUM_MAPPING.put('五', 5); NUM_MAPPING.put('六', 6); NUM_MAPPING.put('七', 7); NUM_MAPPING.put('八', 8); NUM_MAPPING.put('九', 9); } /** * 获取章节号 * * @param chapterName 章节名称 样例:第1203章 或 第一千二百零三章 * @return */ public static int getChapterNum(String chapterName) { chapterName = StringUtils.trim(chapterName); int index = StringUtils.indexOf(chapterName, "章"); if (index <= 0) { return 0; } String chapterNum = StringUtils.substring(chapterName, 0, index); StringBuilder sb = new StringBuilder(); for (int i = 0; i < chapterNum.length(); i++) { char num = chapterNum.charAt(i); if (Character.isDigit(num)) { sb.append(num); } else if (NUM_MAPPING.get(num) != null) { sb.append(NUM_MAPPING.get(num)); } else if (i == (chapterNum.length() - 1)) { if (num == '十') { sb.append("0"); } else if (num == '百') { sb.append("00"); } else if (num == '千') { sb.append("000"); } else if (num == '万') { sb.append(0000); } else { return 0; } } } return Integer.valueOf(sb.toString()); } }
这里对最后一位做了一下单独处理,如九十章,一千章等,只处理到万,一本书的章节应该不可能达到十万计。