From this string: "/resources/pages/id/AirOceanFreight.xhtml"
I need to retrieve two sub-strings: the string after pages/ and the string before .xhtml.
/resources/pages/ is constant. id and AirOceanFreight varies.
Any help appreciated, thanks!
I like Jakarta Commons Lang StringUtils:
String x = StringUtils.substringBetween(string, "/resources/pages/", ".xhtml");
Or, if ".xhtml" can also appear in the middle of the string:
String x = substringBeforeLast(
substringAfter(string, "/resources/pages/"), ".xhtml");
(I also like static imports)
An alternative without jakarta commons. Using the constants:
private final static String PATH = "/resources/pages/";
private final static String EXT = ".xhtml";
you'll just have to do:
String result = filename.substring(PATH.length(), filename.lastIndexOf(EXT));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With