-1

Suppose I have a method called withoutX that, when given a string, removes first and last letters of the string if they are x.

My code is:

public String withoutX(String str) {
  if(str.length()>0 && str.substring(0,1).equals("x")) {
    str = str.substring(1);
  }
  if(str.length()>0 && str.substring(str.length()-1).equals("x")) {
    str = str.substring(0,str.length()-1);
  }
  return str;
}

Why does this return the empty string ("") when str = "x"? Wouldn't the case when str = "x" fail after the first if statement because str = str.substring(1) references an index that is out of bounds (the max. index of str = "x" is 0)?

0

2 Answers 2

3

Wouldn't the case when str = "x" fail after the first if statement because str = str.substring(1) references an index that is out of bounds (the max. index of str = "x" is 0)?

No, because it's not out of bounds: the first (and second) parameters of substring can be anything up to the length() of the string, not just length() - 1. From the Javadoc:

[Throws] IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.

That's larger than, not larger than or equal to.


It would be easier (and more efficient) to write this as:

int start = str.startsWith("x") ? 1 : 0;
int end = str.length() - (str.endsWith("x") && start < str.length() ? 1 : 0);
return str.substring(start, end);

The reason it's easier is that you're simply checking whether the string starts/ends with x; String provides methods to do that without constructing a substring first.

The reason it's more efficient is that it doesn't create intermediate substrings.

3
  • Also, ? : has the lowest (except for =) precedence, so there is never a need to parenthesize the condition, but often a need to parenthesize the ? : construct, i.e. you need str.length() - (str.endsWith("x") ? 1 : 0).
    – Andreas
    May 14, 2017 at 20:22
  • @Andreas typo. Fixed. May 14, 2017 at 20:48
  • 1
    You still didn't fix parenthesis to correct for operator precedence, so that code will not compile. Also, the updated && start == 0 means that it'll never remove trailing x, if there is also a leading x. Perhaps you meant this? (str.endsWith("x") && start < str.length() ? 1 : 0)
    – Andreas
    May 14, 2017 at 20:52
0

Let's go thorough the code step by step:

//str = "x"
if(str.length()>0 && str.substring(0,1).equals("x")) { // both true
     str = str.substring(1);
     //now: str = ""
}
if(str.length()>0 && str.substring(str.length()-1).equals("x")) { // str.length == 0. 
    //Since && does not evaluate the second parameter, everything is fine and if doesn't get called
}
return ""; //as str = ""
1
  • OP is asking why str = str.substring(1) is not failing. You're not answering the question.
    – Andreas
    May 14, 2017 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.