?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Delete Character at Position in String

APCSA-YGJRTV

Which of the following methods will accept a string and a valid position in the string as parameters and return the string with the character at that position removed?

Examples:

stringDelete("greatness", 2) will return "gratness";
stringDelete("xy",0) will return "y";
stringDelete("", 0) will return "";

I.

    public static String stringDeleteI(String str, int pos)    
    {
        if (str.length()>0)
            return str.substring(0, pos) + str.substring(pos+1);
        return str;
    }

II.

    public static String stringDeleteII(String str, int pos)    
    {
         String out = "", letter = "";
         if (str.length()>0)
             letter = str.substring(pos, pos+1);

         for (int i=0; i<str.length(); i++)
         {
             if (!str.substring(i,i+1).equals(letter))
                 out += str.substring(i,i+1);
         }
         return out;
     }

III.

    public static String stringDeleteIII(String str, int pos)
    {
        String out = "";

        for (int i=0; i<str.length(); i++)
        {
            if (i!=pos)
                out += str.substring(i,i+1);
        }
        return out;
    }
A

I only.

B

II only.

C

III only.

D

I and III only.

E

II and III only.