1

I want to remove last three character of filenames. To rename any files, i'm using:

String str = ""; // Here set File name, i.e. listFile.getName();  
String[] tempFile = str.split("[$^txt]");

This regex remove last three character it's OK, but when passing any file name containing "t OR x" the regex remove it. For example:

1) fileName.txt   -> result after split -> fileName -> OK. 
2) textfile.txt   -> result after split -> efile. -> remove any t OR x in file name.

My Q is : How i can remove only last three char.

Thank you

dan
  • 741
  • 9
  • 14
  • 1
    `str.split(".txt$");`? – cs95 Jul 07 '17 at 19:43
  • 3
    `[$^txt]` is a character class. It reads `$` _or_ `^` _or_ `t` _or_ `x` _or_ `t`. And it splits on any _one_ of those characters. –  Jul 07 '17 at 19:56
  • 1
    We're all assuming it's the file extension `.txt`. Do you want to remove a file extension or do you really want to remove the last 3 characters. So if a file name is `myNameIsBob` you will get `myNameIs` or do if the file name is `myNameIsBob.txt`, do you want `myNameIsBob`? – jiveturkey Jul 07 '17 at 19:59
  • have you thought of using Apache Commons IO? it definitly fits your purpose - https://stackoverflow.com/questions/3571223/how-do-i-get-the-file-extension-of-a-file-in-java – Simon Schrottner Jul 07 '17 at 20:03
  • sometimes extensions can be longer than 3 characters too... excel.xlxs do you really just want to dump the extensions? – sniperd Jul 07 '17 at 20:59
  • do you want myNameIsBob? Yes, that what i need . – abdulrahmanAbdullah Jul 07 '17 at 21:55
  • The last dot can be found with this regex: _Find_ `"\\.([^.]*)$"` replace `". –  Jul 09 '17 at 17:30
  • use this: str.split(".txt"); – Vishal Shevale Aug 05 '17 at 17:10

3 Answers3

4

If you want to remove the file's extension a regex is much too complicated for the task. You can work with String.lastIndexOf(int) instead to determine the position of the file extension:

public static String extractFileName(final String fileName)
{
    final int dotIndex = fileName.lastIndexOf('.');
    return dotIndex < 0 ? fileName : fileName.substring(0,dotIndex);
}
Björn Zurmaar
  • 826
  • 1
  • 9
  • 22
  • I don't see anything complicated about a regex. Like `\.([^.]*)$`. I guess if you don't know regex at all any regex you see is too complicated. –  Jul 09 '17 at 17:32
  • Complicated in the sense that the complexity of the solution does not correspond to the complexity of the original requirement. Starting a regular expression engine to find a dot in a string is just a waste of resources. Knowing regular expressions also means when not to use regular expressions. – Björn Zurmaar Jul 09 '17 at 17:38
  • I'm not sure what you mean. Simply put, if `.*(?=\.)` matches, return it. Or is last index and substring really a better solution. Nah, it isn't. –  Jul 09 '17 at 20:52
  • Testing your solution on my machine 1.000.000 times takes about 240 - 270 ms. I even precompiled the pattern to have a fair competition. Testing my solution the same 1.000.000 times takes 20 - 30 ms. That's a factor of about 10. I hope that clarifies my point. – Björn Zurmaar Jul 09 '17 at 21:12
0

Try this RegEx instead:

 ^\\(.+\\)*(.+)\.(.+)$
dan
  • 741
  • 9
  • 14
Akash
  • 587
  • 5
  • 12
0

You could simply just use substring. String.substring is used for this purpose. In your case, you could simply use str.substring(0, str.length - 3) to get everything but the last 3 characters from the string. Besides this, you could use str.split.