Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string question

 
kambar bek
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of substring, try the charAt method.

 
kambar bek
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys, it was silly mistake


i found it after i did it


thanks every1
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic