Java String Tail tail(String text, String separator)

Here you can find the source of tail(String text, String separator)

Description

gets the tail part of the given text after the given separator.

License

Open Source License

Parameter

Parameter Description
text a parameter
separator a parameter

Return

the tailing part, if the separator exists within the given text; otherwise the incoming, unmodified text.

Declaration

public static String tail(String text, String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 flowr.org - all rights reserved. This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License (EPL) v1.0. The EPL is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: flowr.org - initial API and implementation
 ******************************************************************************/

public class Main {
    /**// ww  w  .  ja v  a 2s.  c  om
     * gets the tail part of the given text <b>after</b> the given separator.
     *
     * @param text
     * @param separator
     * @return the tailing part, if the separator exists within the given text; otherwise the incoming, unmodified text.
     */
    public static String tail(String text, String separator) {
        int pos = text.lastIndexOf(separator);
        return pos != -1 ? text.substring(pos + 1) : text;
    }

    /**
     * gets the substring of the given text reduced by the given leading part.
     *
     * @param text
     * @param leadingPart
     * @return the reduced substring, if the text starts with the given leading part; otherwise the incoming, unmodified
     *         text.
     */
    public static String subString(String text, String leadingPart) {
        if (text.startsWith(leadingPart)) {
            return text.substring(leadingPart.length());
        } else {
            return text;
        }
    }

    /**
     * gets the substring of the given text reduced by the number of leading and tailing characters.
     *
     * @param text the String to reduce
     * @param lead number of characters to remove at the leading end
     * @param tail number of characters to remove at the tailing end
     * @return the reduced substring, reduced by <code>lead</code> chars at the begin and <code>tail</code> chars at the
     *         end.
     * @throws StringIndexOutOfBoundsException
     */
    public static String subString(String text, int lead, int tail) {
        return text.substring(lead, text.length() - tail);
    }
}

Related

  1. tail(final String text, final char ch)
  2. tail(final String text, final char ch)
  3. tail(String s, char ch)
  4. tail(String s, String separator)
  5. tail(String src, String dividerRegex)
  6. tailOf(String s, char separator)
  7. tailOfString(String input, String sub)