【Java】+字符串

https://www.cnblogs.com/alisapan/p/6490590.html

https://www.jb51.net/article/144305.htm

大小写转换:https://www.cnblogs.com/zhang12354/p/7944393.html

一、截取两个关键字之间的字符串

1、封装的方法

    /**
     * 根据多个常量关键字读取日志中的动态关键字
     *
     * @param goalStr      被读取的日志字符串
     * @param startKeyword 开始读取的关键字
     * @param endKeyword   结束读取的关键字
     * @return 返回startKeyword与endKeyword之间的关键字
     */
    public String readLogKeywordByKeyword(String goalStr, String startKeyword, String endKeyword) {
        //step1:定位开始关键字字符串的首字符在目的字符串中的位置
        int strFirstIndex = goalStr.indexOf(startKeyword);

        //step2:从目的字符串中截取开始关键字及之后的所有字符
        String resultStr = goalStr.substring(strFirstIndex);

        //step3:把step2中的字符串作为目的字符串,截取某关键字之前的所有字符
        resultStr = StringUtils.substringBefore(resultStr, endKeyword);

        return resultStr;
    }

2、调用测试

    @Test
    public void testStr() {
        ReadLogContent readLogContent = new ReadLogContent();
        String str = "creativeId=CREATIVE201908021000016055;contentData={};],;success=true;needRetry=false;errorContext=null;";
        System.out.println(readLogContent.readLogKeywordByKeyword(str, "success", ";"));//3456789abc
    }

3、调用结果

二、分割字符串(并实现分割字符去重功能)

原始字符串:"  Mem:            4194304    4134400      59904          0          0    2572228"
需求:把原始字符串中的有效字符提取出来(有效字符指:非空白字符) 即预期为“Mem:”,“4194304”,“4134400”,“59904”,“0”,“0”,“2572228”这么7个值
    /**
     * 功能:按照指定的分割字符来分割字符串,含去重功能(去除重复的分割字符)
     * 如:
     * 原始字符串:"  Mem:            4194304    4134400      59904          0          0    2572228"
     * 处理后(List):[Mem:, 4194304, 4134400, 59904, 0, 0, 2572228]
     *
     * @param rawStr   原始字符串
     * @param splitStr 以此字段作为分隔项
     * @return 返回处理后的List列表
     */
    public static List<String> splitString(String rawStr, String splitStr) {
        String[] split = rawStr.split(splitStr);
        List list = Arrays.asList(split);
        List lisrStr = new ArrayList(list);

        for (int i = 0; i < lisrStr.size(); i++) {
            lisrStr.remove("");
            if (lisrStr.get(i).equals("")) {
                i = 0;
            }
        }

        return lisrStr;
    }
原文地址:https://www.cnblogs.com/danhuai/p/10978487.html