Ask Question
11 June, 20:32

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".

+4
Answers (1)
  1. 11 June, 22:14
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers