3

Currently I am using code like this

    while (fileName.endsWith(".csv")) {
        fileName = fileName.substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV));
        if (fileName.trim().isEmpty()) {
            throw new IllegalArgumentException();
        }
    }

The above code works fine when user specifies extension in small letters(.csv),But windows accepts extensions case sensitive so he can give like .CsV ,.CSV etc. how can I alter above code ?

Thanks in Advance

Ganesh H
  • 1,649
  • 3
  • 14
  • 20

6 Answers6

13

why don't you turn it to lowercase?

while (fileName.toLowerCase().endsWith(".csv")) {
    fileName = fileName.substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
    if (fileName.trim().isEmpty()) {
        throw new IllegalArgumentException();
    }
}
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • `filename.lastIndexOf(...)` has the same problem. This must also be converted to lower case. Besides that: This is the answer! – Seelenvirtuose Sep 25 '14 at 05:56
  • @Seelenvirtuose this is the answer, unless you need to preserve case in the file name, which is rather likely. – Thomas Stets Sep 25 '14 at 05:58
  • @ThomasStets With `fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV)` you are _not_ changing the variable. You are only changing the calculation of the index. The variable `fileName` is still substituted with a substring of the _original_ string (`fileName.substring(...)`). – Seelenvirtuose Sep 25 '14 at 05:59
  • @ThomasStets: indeed, this answer does not subvert the fileName variable whatsoever since Strings **are immutable**. 1+ to the answer. – Hovercraft Full Of Eels Sep 25 '14 at 19:00
5

Late night regex solution:

Pattern pattern = Pattern.compile(".csv", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(fileName);
while (matcher.find()) {
    fileName = fileName.substring(0, matcher.start());
    if (fileName.trim().isEmpty()) {
        throw new IllegalArgumentException();
    }
}

The Matcher will only find() once. It can then report its start position which you can use to substring the original file name.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
4

You can try this way

 int lastIndexOfDot=fileName.lastIndexOf("\\.");
 String fileExtension=fileName.substring(lastIndexOfDot+1,fileName.length()); 
 while(fileExtension.equalsIgnoreCase(".csv")){

 } 

Or

while(fileName.toUpperCase().endsWith(".CSV"){}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
4

Please convert to lowercase and then compare.

  while (fileName.toLowerCase().endsWith(".csv")) {
        fileName = fileName.toLowerCase().substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
        if (fileName.toLowerCase().trim().isEmpty()) {
            throw new IllegalArgumentException();
        }
    }
Prashant Sarvaiya
  • 364
  • 1
  • 3
  • 14
3

You can convert both to uppercase.

So change this line

fileName = fileName.substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV));

to

fileName = fileName.toUpperCase().substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV.toUpperCase()));
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
0

use this utility function:

public static boolean endsWithIgnoreCase(String str, String suffix)
{
    int suffixLength = suffix.length();
    return str.regionMatches(true, str.length() - suffixLength, suffix, 0, suffixLength);
}

now you can do:

while (endsWithIgnoreCase(fileName, ".csv")) {
    fileName = fileName.substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
    if (fileName.trim().isEmpty()) {
        throw new IllegalArgumentException();
    }
}
Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34