Get substring from right - Java java.lang

Java examples for java.lang:String Substring

Description

Get substring from right

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        int len = 4;
        System.out.println(right(str, len));
    }/*from w  w  w. j a v  a  2s .c om*/

    /**
     * Get substring from right
     * @param str string
     * @param len length
     * @return substring from right
     */
    public static String right(String str, int len) {
        if (!nullOrEmpty(str) && str.length() > len) {
            return str.substring(str.length() - len);
        }
        return str;
    }

    public static boolean nullOrEmpty(String s) {
        return (s == null) ? true : "".equals(s.trim());
    }

    public static String trim(String s) {
        return emptyIfNull(s).trim();
    }

    public static String emptyIfNull(String s) {
        return (s == null) ? "" : s;
    }
}

Related Tutorials