String example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));
Answer from Sébastien Le Callonnec on Stack OverflowString example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));
A very simple implementation with String.split():
String path = "/abc/def/ghfj.doc";
// Split path into segments
String segments[] = path.split("/");
// Grab the last segment
String document = segments[segments.length - 1];
The accepted answer is correct but it doesn't tell you how to use it. This is how you use indexOf and substring functions together.
String filename = "abc.def.ghi"; // full file name
int iend = filename.indexOf("."); //this finds the first occurrence of "."
//in string thus giving you the index of where it is in the string
// Now iend can be -1, if lets say the string had no "." at all in it i.e. no "." is found.
//So check and account for it.
String subString;
if (iend != -1)
{
subString= filename.substring(0 , iend); //this will give abc
}
You can just split the string..
public String[] split(String regex)
Note that java.lang.String.split uses delimiter's regular expression value. Basically like this...
String filename = "abc.def.ghi"; // full file name
String[] parts = filename.split("\\."); // String array, each element is text between dots
String beforeFirstDot = parts[0]; // Text before the first dot
Of course, this is split into multiple lines for clairity. It could be written as
String beforeFirstDot = filename.split("\\.")[0];
Assuming you are trying to find parent location of specified file simplest way would be using File class or Path instead of String methods. Your code will be more readable and probably safer.
Using java.io.File:
String location = "/abc/def/ghfj.doc";
File f = new File(location);
String parentName = f.getParentFile().getName();
System.out.println(parentName);
Using java.nio.file.Path:
String location = "/abc/def/ghfj.doc";
Path p = Paths.get(location);
String parent = p.getParent().getFileName().toString();
System.out.println(parent);
Output in both cases: def
In case of selecting def in /abc/def/ghfj/ijk/lmn.doc you could use Path#getName(N) where N is zero-based index of elements from farthermost ancestor to selected file like abc is 0, def is 1,...
So your code can look like:
String location = "/abc/def/ghfj/ijk/lmn.doc";
Path p = Paths.get(location);
String parent = p.getName(1).getFileName().toString();
System.out.println(parent);// Output: def
Quick and dirty solution using a regular expression and groups:
public class AClass {
private static final String TEXT = "/abc/def/ghfj/ijk/lmn.doc";
private static final String REGULAR_EXPRESSION = "(/[^/]*){2}/([^/]*)/.*";
public static void main(final String[] args) {
final Pattern pattern = Pattern.compile(REGULAR_EXPRESSION);
final Matcher matcher = pattern.matcher(TEXT);
if (matcher.matches()) {
// the following variable holds "ghfj"
String value = matcher.group(2);
System.out.println(value);
}
}
}
Now you need to be more precise in order to allow us to fine tune the regular expression to your concrete needs.
Edit: I edited the solution according to your own edit. The regular expression has to be understood as follows:
(/[^/]*){2}: two times the character/followed by any character except//: an additional/character([^/]*): a group of characters not containing//.*: a/character followed by any other character
Then matcher.group(2) returns the String held by the group represented by the ([^/]*) part of the above regular expression.