Here you can find the source of substringBefore(String str, String separator)
public static String substringBefore(String str, String separator)
//package com.java2s; //License from project: Apache License public class Main { private static final String EMPTY = ""; public static String substringBefore(String str, String separator) { if (isEmpty(str) || separator == null) { return str; }//from w w w. j a v a 2 s . com if (separator.length() == 0) { return EMPTY; } int pos = str.indexOf(separator); if (pos == -1) { return str; } return str.substring(0, pos); } public static boolean isEmpty(String str) { return str == null || str.length() == 0; } public static String substring(String str, int start) { if (str == null) { return null; } // handle negatives, which means last n characters if (start < 0) { start = str.length() + start; // remember start is negative } if (start < 0) { start = 0; } if (start > str.length()) { return EMPTY; } return str.substring(start); } }