Java How to remove the specified character at the beginning of the specified string

  • 2021-11-10 09:43:27
  • OfStack

Java Removes the specified character at the beginning of the specified string


/**
     *  Removes the specified character at the beginning of the specified string 
     * @param stream  Original string 
     * @param trim  String to delete 
     * @return
     */
    public static String StringStartTrim(String stream, String trim) {
        // null Or empty strings 
        if (stream == null || stream.length() == 0 || trim == null || trim.length() == 0) {
            return stream;
        }
        //  End position of string to delete 
        int end;
        //  Normal expression 
        String regPattern = "[" + trim + "]*+";
        Pattern pattern = Pattern.compile(regPattern, Pattern.CASE_INSENSITIVE);
        //  Removes the specified character at the beginning of the original string 
        Matcher matcher = pattern.matcher(stream);
        if (matcher.lookingAt()) {
            end = matcher.end();
            stream = stream.substring(end);
        }
        //  Returns the processed string 
        return stream;
    }

Intercept string (remove the first n characters)


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);
}

Parameter meaning

origin String to manipulate count : Remove the number of strings

Related articles: