软件编程
位置:首页>> 软件编程>> java编程>> Java如何去掉指定字符串的开头的指定字符

Java如何去掉指定字符串的开头的指定字符

作者:有梦想的攻城狮  发布时间:2022-05-23 13:28:31 

标签:Java,指定,字符串,字符

Java去掉指定字符串的开头的指定字符


/**
    * 去掉指定字符串的开头的指定字符
    * @param stream 原始字符串
    * @param trim 要删除的字符串
    * @return
    */
   public static String StringStartTrim(String stream, String trim) {
       // null或者空字符串的时候不处理
       if (stream == null || stream.length() == 0 || trim == null || trim.length() == 0) {
           return stream;
       }
       // 要删除的字符串结束位置
       int end;
       // 正规表达式
       String regPattern = "[" + trim + "]*+";
       Pattern pattern = Pattern.compile(regPattern, Pattern.CASE_INSENSITIVE);
       // 去掉原始字符串开头位置的指定字符
       Matcher matcher = pattern.matcher(stream);
       if (matcher.lookingAt()) {
           end = matcher.end();
           stream = stream.substring(end);
       }
       // 返回处理后的字符串
       return stream;
   }

截取字符串(去掉前n个字符)


public static String truncateHeadString(String origin, int count) {
   if (origin == null || origin.length() < count) {
       return null;
   }
   char[] arr = origin.toCharArray();
   char[] ret = new char[arr.length - count];
   for (int i = 0; i < ret.length; i++) {
       ret[i] = arr[i + count];
   }
   return String.copyValueOf(ret);
}

参数含义

  • origin:要操作的字符串

  • count:去掉字符串的数量

来源:https://zhangzehai.blog.csdn.net/article/details/73522041

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com