Given a String
, if the String
begins with "red" or "blue" return that color String
, otherwise return the empty String
public String seeColor(String str) {
if(str.length() <= 3){
if(str.equals("red")){
return "red";
}
return "";
}
else if(str.substring(0, 3).equals("red")){
return "red";
}
else if(str.substring(0, 4).equals("blue")){
return "blue";
}
return "";
}
I really don't like the fact that I'm using same code for "red" twice but I can't get to think out another way. If str
is equal to 3, I have to make the check for "red", right?
Example input: "redxx" - output "red",
"xxred" - output "",
"blueAvenue" - output "blue"