Returns the substring before the first occurrence of a delimiter. - Java java.lang

Java examples for java.lang:String Substring

Description

Returns the substring before the first occurrence of a delimiter.

Demo Code


//package com.java2s;

public class Main {
    /**/*from w ww.j  av a  2s.  co m*/
     * Returns the substring before the first occurrence of a delimiter. The
     * delimiter is not part of the result.
     * 
     * @param string    String to get a substring from.
     * @param delimiter String to search for.
     * @return          Substring before the first occurrence of the delimiter.
     */
    public static String substringBefore(String string, String delimiter) {
        int pos = string.indexOf(delimiter);

        return pos >= 0 ? string.substring(0, pos) : string;
    }
}

Related Tutorials