Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 226079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:28:34+00:00 2026-05-11T19:28:34+00:00

prefix/dir1/dir2/dir3/dir4/.. How to parse the dir1, dir2 values out of the above string in

  • 0

prefix/dir1/dir2/dir3/dir4/..

How to parse the dir1, dir2 values out of the above string in Java?

The prefix here can be:

/usr/local/apache2/resumes

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:28:35+00:00Added an answer on May 11, 2026 at 7:28 pm

    If you want to split the String at the / character, the String.split method will work:

    For example:

    String s = "prefix/dir1/dir2/dir3/dir4";
    String[] tokens = s.split("/");
    
    for (String t : tokens)
      System.out.println(t);
    

    Output

    prefix
    dir1
    dir2
    dir3
    dir4
    

    Edit

    Case with a / in the prefix, and we know what the prefix is:

    String s = "slash/prefix/dir1/dir2/dir3/dir4";
    
    String prefix = "slash/prefix/";
    String noPrefixStr = s.substring(s.indexOf(prefix) + prefix.length());
    
    String[] tokens = noPrefixStr.split("/");
    
    for (String t : tokens)
      System.out.println(t);
    

    The substring without the prefix "slash/prefix/" is made by the substring method. That String is then run through split.

    Output:

    dir1
    dir2
    dir3
    dir4
    

    Edit again

    If this String is actually dealing with file paths, using the File class is probably more preferable than using string manipulations. Classes like File which already take into account all the intricacies of dealing with file paths is going to be more robust.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I prefix bound values in TextBlock controls in a StackPanel without using
Does the this prefix for accessing member variables exist in Java? Here is my
I included PREFIX = /usr/local inside my project file and then I run qmake
Can I prefix all the Liferay tables that it initially creates by doing some
String prefix = ; for (String serverId : serverIds) { sb.append(prefix); prefix = ,;
startsWith(string prefix) requires two strings and searches if both strings start with the same
I've seen questions on how to prefix zeros here in SO. But not the
For a fixed prefix length I can do it like so: [ a${filename:0:2} =
I want to prefix the form title within the form tags, but above the
Most OO languages prefix their interface names with a capital I, why does Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.