File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes string question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string question" Watch "string question" New topic

string question

Erjan Kenjegalee

Joined: Apr 19, 2010
Posts: 22

hi , i m doing coding bat exercises in java.
the question :

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 solution:

what i don't get is why it never goes to * part of code

for example if the string given is "xHix" , it only deletes last x , but the first x is still there ,
and it gives wrong answer "xHi", but it should delete the first "x"

Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
Hi Erjan,

When you apply substring from index 0 to a String, you get the same String.

Only if str = "x" you could satisfy this if statement.

Best regards,

Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Instead of substring, try the charAt method.

Erjan Kenjegalee

Joined: Apr 19, 2010
Posts: 22

thanks guys, it was silly mistake

i found it after i did it

thanks every1
I agree. Here's the link:
subject: string question
It's not a secret anymore!