Java - Write code to Trim off recurring strings from the front of a string.

Requirements

Write code to Trim off recurring strings from the front of a string.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String source = "book2s.com";
        String trim = "book2s.com";
        System.out.println(trimFront(source, trim));
    }//from w ww .  ja  v  a  2 s  . c om

    /**
     * Trims off recurring strings from the front of a string.
     * 
     * @param source
     *            the source string
     * @param trim
     *            the string to trim off the front
     */
    public static String trimFront(String source, String trim) {
        if (source == null) {
            return null;
        }
        if (source.indexOf(trim) == 0) {
            // dp("trim ...");
            return trimFront(source.substring(trim.length()), trim);
        } else {
            return source;
        }
    }
}