Java - Write code to cut string from right

Requirements

Write code to cur string from right

You need to check if the string is ending with the passed in string

Hint

Use String.endsWith() and String.substring() methods

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String srcStr = "book2s.com";
        String cutStr = "com";
        System.out.println(cutRight(srcStr, cutStr));
    }/*from  w  w  w.  j a  v a 2s.  com*/
    public static String cutRight(final String srcStr, final String cutStr) {
        if (srcStr == null || cutStr == null) {
            return srcStr;
        }

        String retStr = srcStr;

        if (retStr.endsWith(cutStr)) {
            retStr = retStr.substring(0, retStr.length() - cutStr.length()); 
                                                                             
        }

        return retStr;
    }
}

Related Exercise