• java 字符串(String)常用技巧及自建方法模块汇总


    1.String类常用方法汇总

    (1)删除字符串的头尾空白符

    public String trim()

    (2)从指定位置截取字符串

    public String substring(int beginIndex, int endIndex)

    注意:

    beginIndex -- 起始索引(包括), 索引从 0 开始。

    endIndex -- 结束索引(不包括)。

    (3)根据匹配给定的正则表达式来拆分字符串

    public String[] split(String regex, int limit)

    参数:<1>regex -- 正则表达式分隔符。  <2>limit -- 分割的份数(参数2往往可以省略)

    注意: . 、 | 和 * 等转义字符,必须得加 \。

    2.字符串转为Arraylist集合的自定义方法

    private static ArrayList<String> stringToArrayList(String str) {
            //1.String.substring() 从指定位置开始到指定位置结束截取字符串
            String strTemp=str.substring(1,str.length()-1);
            //2.String.split() 字符串拆分
            String[] strResult=strTemp.split(", ");
            ArrayList<String> resultList=new ArrayList<>();
            for (int i = 0; i <strResult.length ; i++) {
                resultList.add(strResult[i]);
            }
            return resultList;
        }

    注意:该方法输入为以下格式(ArrayList.toString后就会得到以下形式的字符串):

    [可燃爆粉尘作业场所, 喷涂作业场所, 涉氨制冷企业, 有限空间作业场所]

    输出为ArrayList集合的形式。

  • 相关阅读:
    MySQL 5.7.18 zip 文件安装过程
    Mysql 自定义随机字符串
    JAVA基本类型和引用类型
    初识JSP
    Java导出错误数据
    时序图的使用习惯
    Redis踩坑
    ES踩坑
    代码规范
    Git在公司的使用流程
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10713419.html
Copyright © 2020-2023  润新知