3

i have a list of files in this form:

name_of_file_001.csv
name_of_file_002.csv    
name_of_file_123.csv
or
name_of_file.csv
second_name_of_file.csv

i don't know if the file has 001 or not.
how to take name of file (only name_of_file) in java?

Mauro Midolo
  • 1,841
  • 3
  • 14
  • 34
  • I dont really have the time to write a working answer right now, but have you considered checking with regular expressions? for instance smth like `[a-z]*(?=[0-9]{3})` – posdef Feb 23 '15 at 08:48

4 Answers4

2

Try the following:

int i=0;
while(!fullName.charAt(i).equals('.')&&!fullName.charAt(i).equals('0')){
  i++;
}
String name=fullName.substring(0, i);

Take the string from the beginning of the fullName to the first appearance of . or 0.

EDIT:

Referring to the comments and the case of high numbers greater than 1.. and inspired from this answer:

    int i=0;
    String patternStr = "[0-9\.]";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(fullName);
    if(matcher.find()){
     i=matcher.start();  //this will give you the first index of the regex
    }
    String name=fullName.substring(0, i);

EDIT2:

In the case where there's no Extension and the fullname doesn't match the regex(there's no numbers):

if(matcher.find()){
     i=matcher.start();  //this will give you the first index of the regex
}else {
 i=fullname.length();
}
String name=fullName.substring(0, i);

Or simply we will take all the name.

Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
1

I modified chsdk's solution with respect to mmxx's comment:

int i=0;
while(i < fullName.length() && ".0123456789".indexOf(fullName.charAt(i)) == -1) {
  i++;
}
String name=fullName.substring(0, i);

EDIT: Added

i < fullName.length()
fjf2002
  • 872
  • 5
  • 15
0

This little class solves the problem for all the examples shown in main:

public class Example {

   private static boolean isNaturalNumber(String str)
   {
     return str.matches("\\d+(\\.\\d+)?");
   }

   public static String getFileName(String s) {
        String fn = s.split("\\.")[0];
        int idx = fn.lastIndexOf('_');
        if (idx < 0) {
            return fn;
        }
        String lastPart = fn.substring(idx+1);
        System.out.println("last part = " + lastPart);
        if (isNaturalNumber(lastPart)) {
            return fn.substring(0,idx);
        } else {
            return fn;
        }
    }

    public static void main(String []args){
        System.out.println(getFileName("file_name_001.csv"));
        System.out.println(getFileName("file_name_1234.csv"));
        System.out.println(getFileName("file_name.csv"));
        System.out.println(getFileName("file_name"));
        System.out.println(getFileName("file"));
    }
}

EDIT 1: Replaced the exception-based check with a regex check.

EDIT 2: Handling file names without any underscores.

Eldad Mor
  • 5,405
  • 3
  • 34
  • 46
  • Relying on exceptions in regular execution flow isn't good programming practice. – fjf2002 Feb 23 '15 at 09:00
  • That depends on the times you need to call this function. See here for alternatives: http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java – Eldad Mor Feb 23 '15 at 09:04
0

i resolved the problem in this mode:

nameOfFile.split("\\.")[0].replaceall("_[0-9]*","");

split("\.")[0] remove ".csv" name_of_file_001.csv => name_of_file_001

.replaceall("_[0-9]*","") "remove, if there is, "_001" name_of_file_001 => name_of_file

the result is the name of file only

Mauro Midolo
  • 1,841
  • 3
  • 14
  • 34