Java > String-1 > frontAgain (CodingBat Solution)

Problem:

Given a string, return true if the first 2 chars in the string also appear at the end of the string, such as with "edited".

frontAgain("edited") → true
frontAgain("edit") → false
frontAgain("ed") → true


Solution:

public boolean frontAgain(String str) {
  if (str.length() < 2)
    return false;
  else if (str.substring(0,2).equals(str.substring(str.length()-2, str.length())))
    return true;
  else
    return false;
}


14 comments :

  1. Thank you
    There is a solution

    public boolean frontAgain(String str) {
    return str.length()>=2 && str.substring(0, 2).equals(str.substring(str.length()-2, str.length()));
    }

    ReplyDelete
  2. public boolean frontAgain(String str) {
    if(str.length()>1 && str.charAt(0) == str.charAt(str.length()-2)
    && str.charAt(1) == str.charAt(str.length()-1))
    return true;
    else return false;
    }

    ReplyDelete
  3. public boolean frontAgain(String str) {
    if(str.length()>1 && str.substring(0,2).equals(str.substring(str.length()-2)))
    return true;
    else return false;
    }

    ReplyDelete
  4. public boolean frontAgain(String str) {
    if (str.length() < 2) return false;
    if (str.endsWith(str.substring(0, 2))) return true;
    else return false;
    }

    ReplyDelete
  5. public boolean frontAgain(String str) {
    if (str.length() < 2) return false;
    if (str.endsWith(str.substring(0, 2))) return true;
    else return false;
    }

    ReplyDelete
  6. public boolean frontAgain(String str) {
    if(str.length()<2){
    return false;
    }
    String first2 = str.substring(0,2);
    return str.startsWith(first2) && str.endsWith(first2);
    }

    ReplyDelete
  7. public boolean frontAgain(String str) {
    if(str.length()>1&&str.endsWith(str.substring(0,2)))
    return true;
    return false;
    }

    ReplyDelete
  8. public boolean frontAgain(String str) {
    return (str.length()>1&&str.endsWith(str.substring(0,2)));
    }

    ReplyDelete
  9. public boolean frontAgain(String str) {
    if(str.length()>1){
    String a=str.substring(0,2);
    String b=str.substring(str.length()-2,str.length());
    if(a.equals(b)){
    return true;
    }else return false;
    }else return false;

    }

    ReplyDelete
  10. public boolean frontAgain(String str) {
    if(str.length()<2){
    return false;
    }
    return str.substring(0,2).equals(str.substring(str.length()-2));
    }

    ReplyDelete
  11. public boolean frontAgain(String str) {
    if (str.length()>1 && str.endsWith(str.substring(0,2))) {
    return true;
    }
    return false;
    }

    ReplyDelete
  12. public boolean frontAgain(String str) {
    return (str.length()>=2 && (str.substring(0,2).equals(str.substring(str.length()-2))));
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact