Java - Write code to cut string from right


Write code to cur string from right

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


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


//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String srcStr = "";
        String cutStr = "com";
        System.out.println(cutRight(srcStr, cutStr));
    }//  w ww  . j a  va2  s  .c  o m
    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