简体   繁体   中英

Remove specific words in a string

Given a string = "test20190906.pdf", how can I get only "test.pdf" such that it removes the date from the string by using string.replace or remove ?

Considering the format will always be filename + date + .extension.

您可以使用正则表达式删除类似于任何格式的日期的 连续数字,前提是文件名紧跟日期附加。

"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.

Assuming the date contains only numbers, you can use regex to replace numbers, eg:

String fileNameWithDate = "test20190906.pdf";
String fileName = fileNameWithDate.replaceAll("[0-9]+", ""));
System.out.println(fileName);

If the format of date is "yyyyMMdd" then I suggest go for the simplest solution as also given by @pavelbere. But this solution also assumes that the date always appends in the end of the filename.

String file = "test20190906.pdf"; 
String fileName = file.substring(0, file.length() - 12)+".pdf";
string name = "test20190906.pdf"
name.replaceAll("[0-9]","");

My approach would be to remove all numbers which are 8 digits long and are next to the last dot and replace them with a dot using the regex: (\\d{8})(?!.*\\d\\.)\\.

String filename = "filename12345678.pdf";
filename = filename.replaceAll("(\\d{8})(?!.*\\d\\.)\\.", ".");

You can see this being used and an explanation of what it does here .

If the date can be different lengths then replace the {8} with a * , this enables the date to be any length.

An answer that doesn't use Regex: For filename as the original string:

l = filename.split('.')
l[-2] = l[-2][:-8]
output = '.'.join(l)

This uses the fact that the last '.' will always precede the extension, so the 8 characters prior to this will be dates. As long as we remove those, and put the '.' back in, we have the filename regardless of extension, regardless of characters preceding it, without using Regular Expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM