Java > String-1 > withoutEnd (CodingBat Solution)

Problem:

Given a string, return a version without the first and last char, so "Hello" yields "ell". The string length will be at least 2.

withoutEnd("Hello") → "ell"
withoutEnd("java") → "av"
withoutEnd("coding") → "odin"


Solution:

public String withoutEnd(String str) {
  int len = str.length();
  
  return str.substring(1,len - 1);
}


4 comments :

  1. Replies
    1. try hacking the mainframe with this, 284097504398989849589430853290-4760234768^&^#$@(*&^&$%^&*()(*&^%$#$%

      Delete
  2. public String withoutEnd(String word) {
    return word.substring(1, word.length() - 1);
    }

    ReplyDelete
  3. public String withoutEnd(String str) {
    String put="";
    for (int i = 1; i <str.length()-1 ; i++) {
    put+=str.substring(i,i+1);

    }

    return put;
    }

    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