I got a java question: "The string is set", we return the string of the first two characters, so the string "Hello" returns "He".
If the string is shorter than length 2, return everything that is, so "X" returns "X", and an empty string "" returns an empty string "".
Note that str.length()returns the length of the string.
public String firstTwo(String str) {
if(str.length()<2){
return str;
}
else{
return str.substring(0,2);
}
}
I am wondering if there is another way to resolve this issue?
source
share