Get the string between two characters at particular location in java

1681 views java

I need to extract a number between two underscores. This has to be dynamic. That means , I need the string between 4th and 5th underscore from the left. Ex: gcw_distributor_ams_200_20180419_13_10_57.enrich The string I need is 200

Thank You

answered question

If the number always is between the 4th and the 5th, you could try to split it by _, and pick out the [3] element from the resulting list.

Um, 200 is between the 3rd and 4th underscores (not the 4th and 5th), if I'm reading correctly. A minor detail though; the extraction process will be essentially the same either way...

1 Answer


You can use split method:

String fileName = "gcw_distributor_ams_200_20180419_13_10_57.enrich";
String[] splittedFileName = fileName.split("_");

String requiredText = splittedFileName[3];

posted this

Have an answer?


Please login first before posting an answer.