Ask Question

Write a method appendIfMissing which is passed a String s and a String e (for ending). If s doesn't already end with e, the method returns a new String which consists of the contents of s concatenated with e. It returns s otherwise.

For example, if s refers to the String "lightningbug" and e refers to the String "bug", the method returns "lightningbug".

If s refers to the String "Airplane II", and e refers to the String ": the Sequel", the method returns "Airplane II: the Sequel".

+3
Answers (1)
  1. 28 July, 22:30
    0
    public static String appendIfMissing (String s, String e) {

    if (! s. endsWith (e))

    return s+e;

    return s;

    }

    Explanation:

    Create a method called appendIfMissing takes two strings, s and e

    Inside the method, check if s ends with e - using endsWith method. If it is, append the to s and return the new string. Otherwise, return only the s

    *string1. endsWith (string2) method checks if the string1 ends with string2 and returns either true or false.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a method appendIfMissing which is passed a String s and a String e (for ending). If s doesn't already end with e, the method returns ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers