General-purpose utility function for removing characters from front of string - Java java.lang

Java examples for java.lang:String Strip

Description

General-purpose utility function for removing characters from front of string

Demo Code


//package com.java2s;

public class Main {
    /** General-purpose utility function for removing
     * characters from front of string//from w  w  w  . j  a va 2s  . c o  m
     * @param s The string to process
     * @param c The character to remove
     * @return The resulting string
     */
    static public String stripFront(String s, char c) {
        while (s.length() > 0 && s.charAt(0) == c) {
            s = s.substring(1);
        }
        return s;
    }

    /** General-purpose utility function for removing
     * characters from front of string
     * @param s The string to process
     * @param remove A string containing the set of characters to remove
     * @return The resulting string
     */
    static public String stripFront(String s, String remove) {
        boolean changed;
        do {
            changed = false;
            for (int i = 0; i < remove.length(); i++) {
                char c = remove.charAt(i);
                while (s.length() > 0 && s.charAt(0) == c) {
                    changed = true;
                    s = s.substring(1);
                }
            }
        } while (changed);
        return s;
    }
}

Related Tutorials