2
\$\begingroup\$

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()), ""));
\$\endgroup\$
6
\$\begingroup\$

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

System.out.println(test.substring(0, test.lastIndexOf(":")))
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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