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

本文介绍了一种用于处理具有特定分隔符(如';'或'|')的字符串配置的方法。该方法可以根据指定的位置来过滤字符串,并提供了具体的实现代码示例。
    /**
     * 将具有分割号的字符串配置进行过滤
     * @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";

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

 

转载于:https://www.cnblogs.com/annkiny/p/9370984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值