Hello, this is my first post on this forum. Hopefully, it won't get anyone mad or upset.
My question is a logic question. Say, for example, I have this code here:
String str = "Bill Gates" String a = str.substring(0,str.length())
The output would of course be: "Bill Gate"
My assignment is (Taken from Codingbat. Personal Hoppy, not an actual assignment!):
Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.
Ex: extraEnd("Hello") → "lololo"
extraEnd("ab") → "ababab"
extraEnd("Hi") → "HiHiHi"
My answer would be:
public String extraEnd(String str) { String new2 = str.substring (str.length()-2, str.length()+1); return new2 + new2 +new2; }
But, it is wrong. I know the answer is "String new2 = str.substring (str.length()-2, str.length());" But I do not understand why. Shouldn't it be str.length()+1? based on the logic from the Bill Gates code? When using a substring, wouldn't it be the last number and subtract that number by 1? To make more sense of what I am trying to say: When I use str.length() wouldn't that be the second to last letter and not the last letter? Thanks for the help!