String parsing... need to get a substring

Posted on 2003-03-27
Medium Priority
Last Modified: 2010-03-31
I am making a file explorer.  Now for my file copy method it takes two inputs, a file source and file destination.  All are File objects.
Here is an example of what I need:

String source:  C:\Windows\Test\test.txt
String dest:    C:\Somewhere\Windows\Test\test.txt

The user would have dragged the folder C:\Windows\Test\ to C:\Somewhere\
The file will be an exact copy... but I need to extract that Windows\Test\ and append it to the destination String.

Right now, a user would do the above and the resultant File would be C:\Somewhere\test.txt... I need that extra directory information.  
Question by:m3th0dm4n
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

glottis earned 255 total points
ID: 8222620
if the format always remain the same, you could get rid of the first 3 characters this way:

assuming you have to ditch the first 3 characters like 'c:\' or 'd:\'
String source = c:\windows\test\test.txt.
String newSource = source.substring(3); // this will start to copy all the string starting from the 4th character till the end.

also there is another way:
String source = c:\windows\test\test.txt.
int index = source.indexOf("\"); // return the first position of the '\' in source.
String newSource = source.substring(++index);

Author Comment

ID: 8223079
Yes that did it.  I finally ended up doing this:

File destFile = new File(dest.getParent() + "\\" + src.toString().substring(3));

Thank you.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question