将具有分割号的字符串配置按指定位置进行过滤

    /**
     * 将具有分割号的字符串配置进行过滤
     * @param point 在分割的第几个坑里,0开始
     * @param filterStr 要处理的字符串
     * @return 返回过滤后的字符
     */
    public String calcPoint(int point,String filterStr)
    {
        int count = 0; //被遍历次数
        int nextIndex = 0; //下一个索引
        int lastIndex = 0;//最后一次索引
        while (true) {
            int index = filterStr.indexOf(";", nextIndex);
            if (index < 0) {
                index = filterStr.indexOf("|", nextIndex);
            }
            nextIndex = index + 1;
            if (point == count) {
                if (index == -1 && filterStr.length() > 0) {
                    nextIndex = filterStr.length();
                    lastIndex -= 1;
                }
                String result = filterStr.substring(0, lastIndex) + filterStr.substring(nextIndex, filterStr.length());
                return result;
            }
            lastIndex = nextIndex;
            count++;
        }
    }

此问题主要是解决获得了某个位置,处理相同的数量分割的配置的字符串。

例如:

String cfg1 = "210024;210027;210028;210022;210023";

String cfg2="1|1|1|1|1";

String cfg4="1;3;2;4;5";

都是为了过滤对应的配置。

猜你喜欢

转载自www.cnblogs.com/annkiny/p/9370984.html