Get right Most sub string by count - Java java.lang

Java examples for java.lang:String Substring

Description

Get right Most sub string by count

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String sourceString = "java2s.com";
        int count = 42;
        System.out.println(rightMost(sourceString, count));
    }/*w  ww .  j a  v  a  2 s .  c  o  m*/

    static public String rightMost(String sourceString, int count) {
        String result = "";

        if (!isEmpty(sourceString)) {
            int sourceLen = sourceString.length();
            int targetLen = (sourceLen > count) ? count : sourceLen;

            char[] buffer = new char[targetLen];

            sourceString.getChars(sourceLen - targetLen, sourceLen, buffer,
                    0);
            result = new String(buffer);
        }
        return result;
    }

    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }

    public static int length(String source) {
        int result = 0;
        if (isNotEmpty(source)) {
            result = source.length();
        }
        return result;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }
}

Related Tutorials