#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Java string method


    Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.

    withoutX("xHix") → "Hi"
    withoutX("xHi") → "Hi"
    withoutX("Hxix") → "Hxi"
    --------------------------------------------------------------------------------------------
    MY ANSWER
    String result = "";
    public String withoutX(String str) {
    if(str.length()>2){
    if(str.substring(0,1).equals("x") || str.substring(str.length()).equals("x")){
    return str.substring(1,str.length());
    }
    }
    if(str.length()<=2){
    return result;
    }

    return str;
    }
    ----------------------------------------------------------------------------------------
    THIS METHOD IS BEING TESTED WITH THE FOLLOWING :
    expected result your result
    withoutX("xHix") → "Hi" "Hix" X
    withoutX("xHi") → "Hi" "Hi" OK
    withoutX("Hxix") → "Hxi" "Hxix" X
    withoutX("Hi") → "Hi" "" X
    withoutX("xxHi") → "xHi" "xHi" OK
    withoutX("Hix") → "Hi" "Hix" X
    withoutX("xaxbx") → "axb" "axbx" X
    withoutX("xx") → "" "" OK
    withoutX("x") → "" "" OK
    withoutX("") → "" "" OK
    withoutX("Hello") → "Hello" "Hello" OK
    withoutX("Hexllo") → "Hexllo" "Hexllo" OK
    other tests OK
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Did you have a question?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    how do i edit my question?

    sorry,this is my first post.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I have an Edit button beneath my posts. Maybe the forum doesn't allow edits for first posters.

    If you need to, Make a new post with the code in code tags.

IMN logo majestic logo threadwatch logo seochat tools logo