5 ms·
You might want to edit your post prepending all lines of code with an indent (I use four spaces, but even two I think work), ie. public String withoutX(Str
by klibertp 10y ago
You might want to edit your post prepending all lines of code with an indent (I use four spaces, but even two I think work), ie.
public String withoutX(String str) { //handles single x characters
if(str.length() == 1 && str.charAt(0) == 'x'){
str = "";
//handles empty strings
}else if(str.length() == 0){
str = "";
//handles all other cases
}else{
if(str.charAt(0) == 'x'){
str = str.substring(1);
}
if(str.charAt(str.length() - 1) == 'x'){
str = str.substring(0, str.length() - 1);
}
}
return str;
}
public String withoutX(String str) {
if (str.length() > 0 && str.charAt(0) == 'x') {
str = str.substring(1);
}
if (str.length() > 0 && str.charAt(str.length()-1) == 'x') {
str = str.substring(0, str.length()-1);
}
return str;
// Solution notes: check for the 'x' in both spots. If found, use substring()
// to grab the part without the 'x'. Check that the length is greater than 0
// each time -- the need for the second length check is tricky to see.
// One could .substring() instead of .charAt() to look into the string.
}