Get substring After Last separator - Java java.lang

Java examples for java.lang:String Substring

Description

Get substring After Last separator

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        String separator = "o";
        System.out.println(substringAfterLast(str, separator));
    }//from  w ww. j av a2s  . c  o m

    public static String substringAfterLast(String str, String separator) {
        if (str == null || str.length() == 0) {
            return str;
        }
        if (separator == null || separator.length() == 0) {
            return "";
        }
        int pos = str.lastIndexOf(separator);
        if (pos == -1 || pos == (str.length() - separator.length())) {
            return "";
        }
        return str.substring(pos + separator.length());
    }
}

Related Tutorials