remove Any Starting String - Java java.lang

Java examples for java.lang:String Strip

Description

remove Any Starting String

Demo Code


//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    /**//  w  w w.j  av  a 2s.c o m
     * @param stringList iterable of possible starting words
     * @param str String to be examined
     * @return String without the starting element, matched case insensitively
     */
    public static String removeAnyStartingString(
            Iterable<String> stringList, String str) {
        for (String startingString : stringList) {
            Pattern pattern = Pattern.compile("^" + startingString + ".*$",
                    Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
            if (pattern.matcher(str).matches()) {
                Pattern replacementPattern = Pattern.compile(
                        startingString, Pattern.CASE_INSENSITIVE
                                | Pattern.UNICODE_CASE);
                return replacementPattern.matcher(str).replaceFirst("")
                        .trim();
            }
        }
        return str;
    }
}

Related Tutorials