Remove specific words in a string
You can use a regex to remove consecutive digits that resemble a date in any format provided the filename is appended immediately by the date.
"test20190906.pdf".replaceAll("[0-9]{8}\\.", "."));
I see previous answers and that answers does not work if you got other numbers in file name for example: 01_test20190913.pdf
In that case solution will be
String file = "01_test20190913.pdf";
System.out.println(file.substring(0, file.length() - 12)+".pdf");
here i take the first part of string without last 12 characters and add ".pdf"
There are a lot of good answers, but I want present one more. It'll work if filename contains digits not only in date part. I assume that date is always appears before extension and has fixed length.
s.replaceAll("\\d{8}\\.pdf", ".pdf");
And if the file extension varies then you could do some additional work:
public static String removeDate(String s) {
final String extension = s.substring(s.lastIndexOf("."));
final String pattern = "\\d{8}\\" + extension;
return s.replaceAll(pattern, extension);
}
public static void main(String args[])
{
System.out.println(removeDate("test20190101.pdf"));
System.out.println(removeDate("123123test20190101.txt"));
System.out.println(removeDate("123te11st20190101.csv"));
}
This can be done with the regexp only, but at the cost of readability.