获取指令中包含的连续数字

import org.apache.commons.lang.StringUtils;

import org.elasticsearch.common.Strings;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

* Created with IntelliJ IDEA

* Created By Brian

* Date: 2018\3\15

* Time: 16:48

* Desc:根据输入的指令,解析出包含的连续数字

* Vesion:

*/

public class ChineseNumbUtils {

private static final MapchineseMap =new HashMap<>();

    private static final ListcommandPrefs =new ArrayList<>();

    static {

chineseMap.put("一", "1");

        chineseMap.put("元", "1");

        chineseMap.put("壹", "1");

        chineseMap.put("二", "2");

        chineseMap.put("贰", "2");

        chineseMap.put("三", "3");

        chineseMap.put("叁", "3");

        chineseMap.put("肆", "4");

        chineseMap.put("四", "4");

        chineseMap.put("五", "5");

        chineseMap.put("伍", "5");

        chineseMap.put("六", "6");

        chineseMap.put("陆", "6");

        chineseMap.put("七", "7");

        chineseMap.put("柒", "7");

        chineseMap.put("八", "8");

        chineseMap.put("捌", "8");

        chineseMap.put("九", "9");

        chineseMap.put("玖", "9");

        chineseMap.put("〇", "0");

        chineseMap.put("零", "0");

        chineseMap.put("○", "0");

        chineseMap.put("十", "10");

        chineseMap.put("什", "10");

        chineseMap.put("佰", "00");

        chineseMap.put("百", "00");

        chineseMap.put("0", "0");

        chineseMap.put("1", "1");

        chineseMap.put("2", "2");

        chineseMap.put("3", "3");

        chineseMap.put("4", "4");

        chineseMap.put("5", "5");

        chineseMap.put("6", "6");

        chineseMap.put("7", "7");

        chineseMap.put("8", "8");

        chineseMap.put("9", "9");

        commandPrefs.add("第");

        commandPrefs.add("下");

        commandPrefs.add("前");

        commandPrefs.add("后");

        commandPrefs.add("上");

    }

private ChineseNumbUtils() {

}

public static Integer getNumber(String value) {

if (StringUtils.isEmpty(value)) {

return null;

        }

final int[] index = {0};

        commandPrefs.stream().filter(value::contains).forEach(

(String c) -> {

int i =value.lastIndexOf(c);

                    if (index[0] <= i) {

index[0] = i;

                    }

}

);

        char c = value.charAt(index[0] +1);

        if (!chineseMap.keySet().contains(String.valueOf(c))) {

return null;

        }

String str = value.substring(index[0] +1, value.length());

        String[] numbs =new String[str.length()];

        for (int i =0; i < str.length(); i++) {

char s = str.charAt(i);

            if (!chineseMap.keySet().contains(String.valueOf(s))) {

break;

            }

String key =String.valueOf(s);

            if ("十".equals(key) ||"什".equals(key)) {

if (i >0) {

numbs[i] ="0";

break;

                }

}

numbs[i] =chineseMap.get(key);

        }

StringBuilder builder =new StringBuilder();

        for (String numb : numbs) {

if (!Strings.isEmpty(numb)) {

builder.append(numb);

            }

}

return Integer.valueOf(builder.toString());

    }

}

你可能感兴趣的:(获取指令中包含的连续数字)