-4

I have a string:

/abc/def/ghfj/ijk/lmn.doc

I would like to extract ghfj from this.

I tried in this manner: str.substring(str.indexOf("/",9),str.indexOf("/"));

Could someone please provide some help?

  • you can split the string on `/` and then get the 2nd value from the array. – Atri Nov 25 '15 at 22:24
  • 1
    Stack Overflow is not free-code-writing-service. We exchange our effort for your effort, so please show us [what have you tried](http://mattgemmell.com/what-have-you-tried/) so far and explain problems you are facing? – Pshemo Nov 25 '15 at 22:27
  • Also you shouldn't probably use String methods here. Java has classes which can handle manipulating paths quite nicely like `File` on `Path`. – Pshemo Nov 25 '15 at 22:32
  • i tried in this manner: str.substring(str.indexOf("/",8),str.indexOf("/")); – c1hd1901h4 Nov 25 '15 at 22:33
  • Don't post code as comment (it can't be properly formatted there which makes it hard to read). Its place is in your question so use [edit] option and add it to your post. – Pshemo Nov 25 '15 at 22:34
  • Your updated example `/abc/def/ghfj/ijk/lmn.doc` doesn't really match with your code attempt since `str.indexOf("/",8)` is way after `def` (unless you don't want to select `def` here). Please update your question and include logic which which tells which element you want to select. – Pshemo Nov 25 '15 at 23:11

2 Answers2

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
Pshemo
  • 122,468
  • 25
  • 185
  • 269
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.

Kraal
  • 2,779
  • 1
  • 19
  • 36
  • without using split and pattern methods,can't we do this? – c1hd1901h4 Nov 25 '15 at 22:45
  • You're asking how to extract a substring from a String based on a specific condition. That's what regular expressions are for (even if your teacher asks you to find a non robust solution that uses substrings) – Kraal Nov 25 '15 at 22:52
  • Yes my teacher asked to do this by using only string methods – c1hd1901h4 Nov 25 '15 at 23:02