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

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

Description

returns string before last entry of separator in case string doesn't contain separator -- returns whole string

License

Open Source License

Parameter

Parameter Description
str string that should be analyzed
separator string that should separate string. Examples: subStringBeforeLast("aaaQQaQQde","QQ") = "aaaaQQ" subStringBeforeLast("aaaQQaQQde","test") = "aaaQQaQQde"

Declaration


public static String subStringBeforeLast(String str, String separator) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  ww .j  av  a2 s  .c o m
     * returns string before last entry of separator
     * in case string doesn't contain separator -- returns whole string
     *
     * @param str       string that should be analyzed
     * @param separator string that should separate string.
     *                  Examples:
     *                  subStringBeforeLast("aaaQQaQQde","QQ") = "aaaaQQ"
     *                  subStringBeforeLast("aaaQQaQQde","test") = "aaaQQaQQde"
     */

    public static String subStringBeforeLast(String str, String separator) {
        int from = str.lastIndexOf(separator);
        return from == -1 ? str : str.substring(0, from);
    }
}

Related

  1. substringBeforeChar(String str, int separator)
  2. substringBeforeFirst(String string, String delimiter)
  3. subStringBeforeFirstTab(final String s)
  4. substringBeforeLast(final String str, final String separator)
  5. substringBeforeLast(String str, String separator)
  6. substringBeforeLast(String str, String separator)
  7. substringBeforeLast(String text, String separator)
  8. substringBeforeLastChar(String str, String separator)
  9. substringBeforeLastIgnoreCase(final String target, final String separator)