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

Java examples for java.lang:String Strip

Description

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

Demo Code


//package com.java2s;

public class Main {
    /** General-purpose utility function for removing
     * characters from the front and back of string
     * @param s The string to process/*from w ww  .j  a v  a 2  s . com*/
     * @param head exact string to strip from head
     * @param tail exact string to strip from tail
     * @return The resulting string
     */
    public static String stripFrontBack(String src, String head, String tail) {
        int h = src.indexOf(head);
        int t = src.lastIndexOf(tail);
        if (h == -1 || t == -1)
            return src;
        return src.substring(h + 1, t);
    }
}

Related Tutorials