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 go about making my routes recognise an optional prefix parameter as
String prefix = ; for (String serverId : serverIds) { sb.append(prefix); prefix = ,;
Most OO languages prefix their interface names with a capital I, why does Java
How can I remove the author prefix on a WordPress website, I have done
I have a prefix trie. What is the recommended schema for representing this structure
Do you prefix each field in a table with abbreviated table name? Example: Table:
I've had an app doing prefix searches for a while. Recently the index size
I want to store a URL prefix in an Windows environment variable. The ampersands
Is there a way to set the prefix on the Signature of a Signed
It's shown that 'as' casting is much faster than prefix casting, but what about

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.