Java String Sub String substringBefore(String str, String separator)

Here you can find the source of substringBefore(String str, String separator)

Description

substring Before

License

Apache License

Declaration

public static String substringBefore(String str, String separator) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final int INDEX_NOT_FOUND = -1;
    private static final String EMPTY = "";

    public static String substringBefore(String str, String separator) {
        if (isEmpty(str) || separator == null) {
            return str;
        }//from   w w  w.jav  a  2  s. c o  m
        if (separator.length() == 0) {
            return EMPTY;
        }
        int pos = str.indexOf(separator);
        if (pos == INDEX_NOT_FOUND) {
            return str;
        }
        return str.substring(0, pos);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}

Related

  1. substringBefore(final String str, final String separator)
  2. substringBefore(final String string, final String separator)
  3. substringBefore(String input, String sub)
  4. substringBefore(String s, char c)
  5. substringBefore(String str, int endChar)
  6. substringBefore(String str, String separator)
  7. substringBefore(String str, String separator)
  8. substringBefore(String string, String delimiter)
  9. substringBefore(String text, String str)