Remove file prefix in absolute path but prefix is also directory part
java, string, trim
Solution
if you have got it is a File id defo use @goldilocks' approach. But if for some reason you simply have it as a String, first thing that popped into my head is this:
String target = "temp_";
String fullPath = "C:/Dump/sol1/temp_results/temp_2012-04-core.pcap";
StringBuffer sb = new StringBuffer(fullPath);
int end = fullPath.lastIndexOf(target) + target.length();
System.out.println(sb.replace(fullPath.lastIndexOf(target), end, ""));
Problem
I was wondering if there is any nice solution for the following problem: Assuming I have a string with the absolute Path to a file and the file has the prefix "temp_". I am able to trim the prefix with string.replaceFirst(). But if I am unlucky "temp_" is also part of the directory in that String. How to make sure only the last occurence will get trimmed? I can only think to parse it myself, but was wondering if there's magic left to do it a better way? To be more precisely as example: ``` C:\Dump\sol1\temp_results\temp_2012-04-core.pcap ``` Should become: ``` C:\Dump\sol1\temp_results\2012-04-core.pcap ```