package com.walker.openocr.util; public class TextUtils { public static final String EMPTY_VALUE = ""; public static final char DATE_SEPARATOR_MIDDLE_LINE = '-'; public static final char DATE_SEPARATOR_LEAN_LINE = '/'; public static final char COMMA_EN = ','; public static final char COMMA_ZH = ','; public static final boolean isEmpty(Object text){ if(text == null || text.toString().equals(EMPTY_VALUE)){ return true; } return false; } /** * 把原始字符串中移除给定的关键词集合。如:我爱祖国,给定关键词"祖国" --> 我爱 * @param value 原始字符串 * @param key 关键词,会拆分为每个字 * @return */ public static final String removeKeys(String value, String key){ String[] keys = key.split(EMPTY_VALUE); // 拆分为每个字替换为空 for(String k : keys){ value = value.replaceFirst(k, EMPTY_VALUE); } return value; } /** * 从字符串中解析出来两个数字日期。格式如下: *
     *     1) 自2022年3月14日00:00时起至2023年3月13日24:00时止
     *     2) 保险期间自2022年08月25日00时00分起至2023年08月24日24时00分止
     *
     * 
* @param text * @return 返回两个日期:2022-08-25, 2023-08-24,如果日月统一为2位数字 * @author 时克英 * @date 2022-09-06 */ public static final String[] parseDoubleDateWithNumber(String text){ String[] array = {"",""}; if(text == null || text.equals("")){ return array; } boolean startNum = false; // 是否开始一个日期过滤 int count = 0; StringBuilder sb = null; for(char c : text.toCharArray()){ if(count > 1){ System.out.println("已经解析2个日期,结束"); break; } if(c == 32){ // 空格不管 continue; } if(Character.isDigit(c)){ // 遇到数字,只有遇到第一个不为0数字,才开始启动解析 if(!startNum && c != '0'){ startNum = true; sb = new StringBuilder(); } if(startNum){ sb.append(c); continue; } } else if(c == '年' || c == '月' || c == DATE_SEPARATOR_LEAN_LINE || c == DATE_SEPARATOR_MIDDLE_LINE){ sb.append(DATE_SEPARATOR_MIDDLE_LINE); continue; } else if(c == '日'){ array[count] = sb.toString(); count ++; startNum = false; continue; } } // 如果日期中,月、日为一个字符,则替换为2位(补0) String one = null; String[] yearMonthDay = null; StringBuilder temp = null; for(int i=0; i * 1) 保险费合计(人民币大写):壹佰贰拾元整 (Y:120.00元)其中救助基金(1.00%) * 2) RMB284.32元(不含税保费:268.23元,税额:16.09元)(大写)人民币贰佰捌拾 * 3) 总金额为12,006,599.01 * * @param text * @return 返回 120.00元 | 284.32元 */ public static final String parseFirstMoneyNumber(String text){ if(text == null || text.equals("")){ return EMPTY_VALUE; } StringBuilder sb = new StringBuilder(); boolean startNum = false; for(char c : text.toCharArray()){ if(c == 32){ // 空格不管 continue; } if(c == COMMA_EN || c == COMMA_ZH){ continue; } if(Character.isDigit(c)){ if(!startNum && c != '0'){ startNum = true; } if(startNum){ sb.append(c); continue; } } else if(c == '.'){ sb.append(c); continue; } else if(startNum) { // 其他非数字字符,结束判断 startNum = false; break; } } return sb.toString(); } }