Always think of looking for the answer for this sort of question in Apache Commons... the basic ones should probably be included with most builds. The most appropriate (not using regexes) is this:
StringUtils.substringAfterLast(String str, String separator)
But if you really want to use a regex for some reason there are a couple of methods which you might want to use, e.g.
String removeAll(String text, String regex)
or
String removeFirst(String text, String regex)
Amusingly, you can get what you want by doing this (relying on greedy quantifiers):
StringUtils.removeFirst( myString, ".*," );
StringUtils is part of apache commons-lang3.
Apart from anything else there is no point re-inventing the wheel. But I can think of at least 2 other advantages:
- the Apache people can also be counted on to have developed well tested code dealing with many "gotchas".
- these Apache methods are usually well named: you don't have to clutter up your code with stoopid utility methods; instead you have a nice clean method which does what it says on the tin...
PS there's nothing to prevent you looking at the source code to check about what the method actually does. Usually they are very easy to understand.