字符串处理StringUtils方法-字符串截取

时间:2021-09-15
本文章向大家介绍 字符串处理StringUtils方法-字符串截取,主要包括 字符串处理StringUtils方法-字符串截取使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

字符串处理StringUtils方法-字符串截取

StringUtils引入

    <!--StringUtils的引入-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
1.根据指定位置截取字符串,当指定下标大于等于0时从左往右开始截取,小于0时从右往左截取,下标计数同数组下标一致,从0开始
  • //指定起始位置
    public static String substring(final String str, int start)
    
  • //指定开始结束位置 [start,end) start<=起始位置<end

    public static String substring(final String str, int start, int end)
    
2.根据指定字符截取目标字符串 (注:截取到的字符串不包含条件字符)
  • //从分割符第一次出现的位置向前分割 ,不包含该分割符

    public static String substringBefore(final String str, final String separator) 
    
  • //从分割符第一次出现的位置向后分割 ,不包含第一次出现的该分割符

    public static String substringAfter(final String str, final String separator)
    
  • //从分割符最后一次出现的位置向前分割 ,不包含最后一次的该分割符

    public static String substringBeforeLast(final String str, final String separator) 
    
  • //从分割符最后一次出现的位置向后分割 ,不包含该分割符

    public static String substringAfterLast(final String str, final String separator)
    
  • //截取在特定字符区间的字符,第一次出现的字符区间

    public static String substringBetween(final String str, final String open, final String close) 
    

原文地址:https://www.cnblogs.com/xiaoheiqia/p/15271301.html