Java examples for java.lang:String Substring
The following code shows how to substring Before Last
public class Main { public static void main(String[] argv) { String str = "java2s.com"; String separator = "2s.com"; System.out.println(substringBeforeLast(str, separator)); }//from w ww . ja va2 s. c o m public static final int INDEX_NOT_FOUND = -1; public static String substringBeforeLast(String str, String separator) { if (isEmpty(str) || isEmpty(separator)) { return str; } int pos = str.lastIndexOf(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; } }