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