stripping elements of different length from a file name java

Hi: As a company we get a lot of orders sent in csv format by email. I use  a third party  program called 'attachment save'  which runs in outlook and saves all attachments to a particular directory. Since it is not unknown for the files from different customers to have the same name, the program adds the date. Thus an attached file j031251.csv  is saved as J0312512016-01-27 12-11.csv .
I then use java to pick out files from a certain customer (in this case always has "wp201" in his filename:

for (int i = 0; i < files.length; i++) {
            fileName = files(i).getName();   // (i put in brackets not parenthesis)
            //rename file to take date & time off    
             String abc = new String(fileName);
             String xyz = new String(abc.substring(0,(abc.length()-20))+abc.substring((abc.length()-4),abc.length()))  ;            
            //end rename file to take date & time off    
                File currentFile = new File(dir + "\\" + abc);
                           success = currentFile.renameTo(new File(dew + "\\" + xyz));
                    System.out.println("File Copied: "+ currentFile);
                else System.out.println("NOT Copied: " + currentFile);          
Which strips out the date and puts the file in a specific folder. It does this by counting back from the end of the file; easy as the date is always 16 characters.
However, now another customer wants special treatment; but his file is the same as a lot of other peoples files. 'Attachment save' will allow me to insert the senders address like so: " 12-11.csv", where I insert XXX and YYY as delimiters, but can choose anything. Is there a way of stripping out everything after the XXX but BEFORE the ".csv"? This is because , of course, customer addresses will be of different length and file types will be different.
Richard TeasdaleFinancial ControllerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is there a way of stripping out everything after the XXX but BEFORE the ".csv"?

- I'm just curious to ask why you'd want to insert the YYY element for this exceptional case(s), but then strip it out again . . . maybe I have misunderstood smthg.
Richard TeasdaleFinancial ControllerAuthor Commented:
No; I agree we do not need the YYY; just there as a delimiter for my purposes. Thanks!
You can't possibly mean this, can you

String s = " 12-11.csv";
	s = s.substring(0,s.indexOf("XXX"))+s.substring(s.lastIndexOf(".csv"));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Richard TeasdaleFinancial ControllerAuthor Commented:
I most certainly do! May be easy for you but not for a numbnuts like me!
Thank you very much for your prompt and able assistance.
Lucky strike on my part - (I mean I thought I might have read between the lines that it was something more dynamic (after all, you said the file type can change too)). But hey, if it works, great. ;)
Richard TeasdaleFinancial ControllerAuthor Commented:
the file type does change but the range is limited so I can knock out a few  blocks to cover the range of type. Thanks again
Richard TeasdaleFinancial ControllerAuthor Commented:
..but if you are still there, can I change it to cover a range of file types in one pass - ie pick up the last three characters, hold, then add back?
I think you might like to re-open your question, as it looks to me as if a stronger and more flexible fix is needed - perhaps using a Regular Expression.

OR you might like to consider putting it up for a Gig,so that a seasoned expert can provide a proper fix. ;)
mccarlIT Business Systems Analyst / Software DeveloperCommented:
can I change it to cover a range of file types in one pass - ie pick up the last three characters, hold, then add back?

Yes, with one tiny change to Krakatoa's answer above...

s = s.substring(0, s.indexOf("XXX")) + s.substring(s.lastIndexOf("."));

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.