For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
left2:
public String left2(String str) { return str.substring(2) + str.substring(0, 2); }
right2:
public String right2(String str) { int len = str.length(); return str.substring(len - 2) + str.substring(0, len - 2); }
theEnd:
public String theEnd(String str, boolean front) { if (front) return str.substring(0, 1); return str.substring(str.length() - 1); }
withouEnd2:
public String withouEnd2(String str) { if (str.length() <= 2) return ""; return str.substring(1, str.length() - 1); }
middleTwo:
public String middleTwo(String str) { int len = str.length(); return str.substring(len / 2 - 1, len / 2 + 1); }
endsLy:
public boolean endsLy(String str) { int len = str.length(); if (len < 2) return false; return (str.substring(len - 2).equals("ly")); }
nTwice:
public String nTwice(String str, int n) { return str.substring(0, n) + str.substring(str.length() - n); }
twoChar:
public String twoChar(String str, int index) { if (index < 0 || index + 2 > str.length()) return str.substring(0, 2); return str.substring(index, index + 2); }
middleThree:
public String middleThree(String str) { int len = str.length(); if (len == 3) return str; return str.substring(len / 2 - 1, len / 2 + 2); }
hasBad:
public boolean hasBad(String str) { if (str.length() <= 2) return false; if (str.length() == 3) return str.substring(0, 3).equals("bad"); return str.substring(0, 3).equals("bad") || str.substring(1, 4).equals("bad"); }
For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.