String example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));
Answer from Sébastien Le Callonnec on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_substring.asp
Java String substring() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The substring() method returns a substring from the string.
🌐
Crunchify
crunchify.com › java j2ee tutorials › in java how to get all text after special character from string?
In Java How to Get all Text After Special Character from String? • Crunchify
November 8, 2021 - package crunchify.com.java.tutorials; /** * @author Crunchify.com * Program: Get everything after Special character like _ or : or * from String in java * */ public class CrunchifyGetStringAfterChar { public static void main(String[] args) { String crunchifyStr = "Hey.. This is Crunchify.com"; String crunchifyStr2 = "HELLO_THIS_IS_CRUNCHIFY_COM"; String crunchifyStr3 = "This is simple substring example *"; // substring(): Returns a string that is a substring of this string.
🌐
Baeldung
baeldung.com › home › java › java string › get substring from string in java
Get Substring from String in Java | Baeldung
July 21, 2024 - For more details on the Java regular expressions check out this tutorial. We can use the split method from the String class to extract a substring. Say we want to extract the first sentence from the example String. This is quite easy to do using split: ... Since the split method accepts a regex we had to escape the period character...
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-string-substring
Master Java Substring Method: Examples, Syntax, and Use Cases | DigitalOcean
February 20, 2025 - substring(int beginIndex, int endIndex): The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is (endIndex - beginIndex). Both the string substring methods can throw IndexOutOfBoundsException if any of the below ...
🌐
Dot Net Perls
dotnetperls.com › between-before-after-java
Java - String Between, Before, After - Dot Net Perls
-1) { return ""; } int adjustedPosA = posA + a.length(); if (adjustedPosA >= posB) { return ""; } return value.substring(adjustedPosA, posB); } static String before(String value, String a) { // Return substring containing all characters before a string. int posA = value.indexOf(a); if (posA == ... 0, posA); } static String after(String value, String a) { // Returns a substring containing all characters after a string.
🌐
W3Docs
w3docs.com › java
In java how to get substring from a string till a character c?
String s = "Hello, world!"; char ... != -1) { String sub = s.substring(0, index); System.out.println(sub); // Outputs "Hello, " } ... The indexOf method returns the index of the first occurrence of the specified character in the string...
🌐
Educative
educative.io › answers › what-is-stringutilssubstringafter-in-java
What is StringUtils.substringAfter in Java?
This method returns the substring that comes after the first occurrence of the separator.
🌐
TutorialsPoint
tutorialspoint.com › get-the-substring-after-the-first-occurrence-of-a-separator-in-java
Get the substring after the first occurrence of a separator in Java
public class Demo { public static void main(String[] args) { String str = "Tom-Hanks"; String separator ="-"; int sepPos = str.indexOf(separator); if (sepPos == -1) { System.out.println(""); } System.out.println("Substring after separator = "+str.substring(sepPos + separator.length())); } }
Find elsewhere
🌐
Coderanch
coderanch.com › t › 563326 › java › find-substring-separator-Java-String
How to find substring after a given separator in Java String (Java in General forum at Coderanch)
programming forums Java Mobile ... this forum made possible by our volunteer staff, including ... ... Hi, Answer: I guess using StringUtils.subStringAfter() function should do this....
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-substring-method-example
Java String substring() Method with examples
Here we have a string and we want to get a substring between two strings “Beginners” and “com”. To do this, we have searched the index of first string using indexOf() and added the length of this string, this is because indexOf() gives the index of first character of the string but we want to start reading after the end of string “Beginners”. Similarly, we found the starting index of second string “com” and get the substring between these indexes using substring method. public class JavaExample{ public static void main(String[] args){ //input string String str = "Welcome to [BeginnersBook.com]"; //get the substring between "Beginners" and "com" int start = str.indexOf("Beginners")+"Beginners".length(); int end = str.lastIndexOf("com"); String outStr = str.substring(start, end); System.out.println(outStr); } }
🌐
Coderanch
coderanch.com › t › 386253 › java › finding-character-specific-word-String
finding the character after a specific word in a String (Java in General forum at Coderanch)
String.split would be a better alternative to StringTokenizer. All you need to remember is that . is a regular expression special character so it will need to be escaped:
Top answer
1 of 2
1

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
2 of 2
0

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.

🌐
CodingTechRoom
codingtechroom.com › question › java-extract-substring-after-character
How to Extract a Substring from a String in Java After a Specific Character? - CodingTechRoom
Extracting a substring from a string in Java after a specific character can be done using the `substring` and `lastIndexOf` methods.
🌐
Hero Vired
herovired.com › learning-hub › blogs › java-substring
Substring in Java: How to Use the Function [Code Examples]
The Java substring(begIndex, endIndex) process extracts a part of the given string. It then returns the new string that contains the original characters of the string from begIndex (inclusive) to endIndex (exclusive).
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.text › substring-after.html
Kotlinlang
Returns true if this char sequence contains the specified other sequence of characters as a substring.
🌐
Intellipaat
intellipaat.com › home › blog › substring in java: examples, methods and applications
What is Substring in Java: Examples and Methods to Extract a Substring
August 5, 2025 - In this example, the substring ... is assigned to the “substring” variable and then printed to the console. The substringAfter() method is provided by the widely-used Apache Commons Lang library in Java....