There are four types of file names:
- Double extension file name
- File name without extension
- File name with a dot at the end and without extension
- A file name with a proper name.
Like this:
String doubleexsension = "doubleexsension.pdf.pdf";
String noextension = "noextension";
String nameWithDot = "nameWithDot.";
String properName = "properName.pdf";
String extension = "pdf";
My goal is to clear all types and correctly output only filename.filetype. I made a little silly script to make this post:
ArrayList<String> app = new ArrayList<String>();
app.add(doubleexsension);
app.add(properName);
app.add(noextension);
app.add(nameWithDot);
System.out.println("------------");
for(String i : app) {
if (i.endsWith(".")) {
String m = i + extension;
System.out.println(m);
break;
}
String p = i.replaceAll("(\\.\\w+)\\1+$", "$1");
System.out.println(p);
}
It is output:
------------
doubleexsension.pdf
properName.pdf
noextension
nameWithDot.pdf
I do not know how I can handle noextension. How should I do it? When there is no extension, it should take a value extensionand bind it to the line at the end.
My desired result:
------------
doubleexsension.pdf
properName.pdf
noextension.pdf
nameWithDot.pdf
Thanks in advance.
Avión source
share