Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

String parsing... need to get a substring

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.  
1 Solution
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);
m3th0dm4nAuthor Commented:
Yes that did it.  I finally ended up doing this:

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

Thank you.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now