0

I am comparing file names in a folder ,I want to remove some part of string and check whether name exists or not

Here is my file name :

file1Name:AB-05012_MM-AB_585859_01 
file2Name:AB-05012_MM-AB_732320_01-1

Now i want to compare the string only till

AB-05012_MM-AB_732320_01 ignore '-1'

Here is my logic

if (file1Name.equals(file2Name.contains(""))){
  list.add(file1Name);           
}
6
  • 3
    Not enough info. Do you only want to modify file names that end in -1? Or - and a digit? Or - and any character? Or - and more than one character? What characters do you want to check for following the -? Or do you want to look for - followed by a digit in the middle of the name? Can anything follow the -1? If so, what? Commented Oct 20, 2015 at 5:26
  • Can you post example search terms and matching/non-matching filenames? Commented Oct 20, 2015 at 6:56
  • I have two folder which contains xml files & other contains .cgm files, Now Both the files have almost same name ,the .cgm files have extra '-00' Commented Oct 20, 2015 at 8:09
  • For each xml we have one or more .cgm files in the folder ,It is like abc.xml has abc-01,abc-02,abc-03 Commented Oct 20, 2015 at 8:09
  • Now i want to check the name and add the all the .cgm files to a list Commented Oct 20, 2015 at 8:09

3 Answers 3

1

When you know there is an extra character in second file name then why not using

fileName2.startsWith ( fileName1)

or

int indexOfDot = fileName1.lastIndexOf(".");
fileName2.startsWith ( fileName1.subString( 0, indexOfDot)

But this is very specific to your problem. or for the cases where
fileName2 = fileName1 + any character or digit

Sign up to request clarification or add additional context in comments.

13 Comments

No i have to check for different file names
Until unless you put more light on your problem, none will be able to give you the suitable solution
I have two folder which contains xml files & other contains .cgm files, Now Both the files have almost same name ,the .cgm files have extra '-00'
For each xml we have one or more .cgm files in the folder ,It is like abc.xml has abc-01,abc-02,abc-03
Now i want to check the name and add the all the .cgm files to a list
|
0

You can try this:

String myStr = str.substring(0, str.lastIndexOf("-1")); 

Comments

0

if your file name is same length:

if (file1Name.equals(file2Name.substring(0,24))){
 //if same to some  task              
}

else:

if (file1Name.equals(file2Name.substring(0,file2Name.lastIndexOf('-')))){
 //if same to some  task              
}

And I think the second solution better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.