Strings In Depth16 / 18
Up next

Strip a prefix

class Main {
    // Java has no removePrefix; compose it yourself
    static String removePrefix(String s, String prefix) {
        return s.startsWith(prefix) ? s.substring(prefix.length()) : s;
    }

    public static void main(String[] args) {
        System.out.println(removePrefix("unhappy", "un"));
        // stripLeading trims whitespace, not arbitrary text
        System.out.println("   padded".stripLeading());
    }
}
What you're solving

startsWith plus substring