Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need to remove last text after : in my String and : too. I have tried this. Please review. If there is a better way to do, please tell me.

String test = "temp:content:region:deposit:up";
System.out.println(test.replace(test.substring(test.lastIndexOf(":"), test.length()), ""));
share|improve this question

1 Answer

up vote 5 down vote accepted

Using regexp (replace) isn't the best solution. I think you should use only substring:

System.out.println(test.substring(0, test.lastIndexOf(":")))
share|improve this answer
Thanks for the info Andrew – Vanathi Apr 23 '12 at 11:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.