Problem:
Given a string, return a version without both the first and last char of the string. The string may be any length, including 0.
withouEnd2("Hello") → "ell"
withouEnd2("abc") → "b"
withouEnd2("ab") → ""
withouEnd2("Hello") → "ell"
withouEnd2("abc") → "b"
withouEnd2("ab") → ""
Solution:
public String withouEnd2(String str) {
int len = str.length();
if (len < 3)
return "";
else
return str.substring(1,str.length()-1);
}
public String withouEnd2(String str) {
ReplyDeleteif(str.length()==1)
return str = "";
if(str.length()>1)
str = str.substring(1,str.length()-1);
return str;
}
public String withouEnd2(String str) {
ReplyDeleteif(str.length() <= 1){
return "";
}
else{
String mid = str.substring(1,str.length() -1);
return mid;
}
}
public String withouEnd2(String str) {
ReplyDeletereturn (str.length() <= 2)? "" : str.substring(1, str.length() - 1);
}
if (str.length()>2)
ReplyDeletereturn str.substring(1,str.length()-1);
else
return "";
if(str.length() > 2) {
ReplyDeletereturn str.substring(1, str.length()-1);
} else
return "";
where is the beginning
Deleteof the code
alternative solution :
ReplyDeletepublic String withouEnd2(String str) {
String s1 = str , s2 = "";
int len = s1.length();
if(len <3){
s2= "";
}else{
s2= s1.substring(1,len-1);
}
return s2;
}