【基础算法--java】字符串分割封装算法

java字符串分割的封装算法:

 

package com.hadoop.ot.util;


import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import com.hadoop.plat.util.SplitValueBuilder;


/**
 */
public class StringUtil {



    public static int MAX_NUM = 1000000;
    /**
     * 根据索引查找指定分隔符之前的字符串
     * @param value
     * @param split
     * @param index
     * @return
     */
    public static String find(String value, String split, int index) {
        if (value == null || split == null || index < 0) {
            return null;
        }
        value = value.trim();
        if (value.length() == 0) {
            return null;
        }
        int counter = 0;
        int slen = split.length();
        int beginIndex = 0;
        int endIndex = 0;
        while ((endIndex = value.indexOf(split, beginIndex)) >= 0) {
            if (counter++ == index) {
                if ((endIndex - beginIndex) == 0) {
                    return "";
                }
                return value.substring(beginIndex, endIndex);
            }
            // 过滤掉仅有分隔符,但是没有内容的情况。如连续的||
            if ((endIndex - beginIndex) == 0) {
                beginIndex += slen;
                continue;
            }
            beginIndex += (endIndex - beginIndex);// tmp.length();
            beginIndex += slen;
        }
        return null;
    }




    /**
     * 从字符串左侧开始,只分隔出指定长度的数组。
     * @param str
     * @param plainSeperator
     * @param len
     * @return
     */
    public static List<String> fastSplitToLimit(String str, String plainSeperator, int len) {


        List<String> result = new ArrayList<String>(len);
        int pos = 0;
        int idx = 0;
        int inc = plainSeperator.length();
        int size = 0;
        while (true) {
            idx = str.indexOf(plainSeperator, pos);
            if (idx < 0) {
                result.add(str.substring(pos, str.length()));
                break;
            }
            // 达到分隔限制,则直接返回剩余内容
            if (++size == len) {
                result.add(str.substring(pos));
                break;
            } ;
            result.add(str.substring(pos, idx));
            pos = idx + inc;


        }
        return result;
    }
    
    /**
     * 从字符串右侧开始,只分隔出指定长度的数组。
     * @param str
     * @param plainSeperator
     * @param len
     * @return
     */
	public static List<String> fastSplitToLimitFromRight(String str, String plainSeperator, int len) {


		List<String> result = new ArrayList<String>(len);
		int pos = 0;
		int idx = 0;
		int stop = str.length();
		int inc = plainSeperator.length();
		int size = 0;
		int start = 0;
		while (true) {


			if (len <= 1) {
				System.err.println("'len' parameter must be > 1!");
				break;
			}
			idx = str.lastIndexOf(plainSeperator, stop - 1);


			if (idx < 0) {
				result.add(str.substring(pos, stop));
				break;
			}
			// 达到分隔限制,则直接返回剩余内容
			if (++size == len) {
				result.add(str.substring(0, start - inc));
				break;
			}
			;
			start = idx + inc;
			result.add(str.substring(start, stop));
			stop = start - 1;


		}
		return result;
	}   
    
    public static void main(String[] args) {
        String v1 = " ";
        String v2 = "b";
        String v3 = "c";
        String v = StringUtil.join(v1, v2, v3);
        System.out.println(v);
        
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值