Coding Bat – delDel

This exercise requires that the program analyze a string to see if it contains the word “del” starting at index position 1. If it does, it is to remove it and display the word with the “del” removed.

Here is the code that you start with:

public String delDel(String str) {

}

As you can see, we have a single string variable named “str”.

public String delDel(String str) {
 if(str.length()>=4 && str.substring(1,4).equals("del")){
 return str.substring(0, 1) + str.substring(4);
 }
 return str;
}

This solution utilizes a conditional if with the Boolean AND to solve the problem.

We start by looking to see if the string is 4 or more characters in length AND if the string starting at index 1 to 4 is “del”.

If this Boolean is TRUE, then we return the string from index 0 to 1 concatenated with the string from index 4 to the end.

If this Boolean is FALSE, then we simply return the string unmodified.

Leave a Reply

Your email address will not be published. Required fields are marked *