Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to split the <code>String</code> at the <code>/</code> character, the <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)" rel="noreferrer"><code>String.split</code></a> method will work:</p> <p>For example:</p> <pre><code>String s = "prefix/dir1/dir2/dir3/dir4"; String[] tokens = s.split("/"); for (String t : tokens) System.out.println(t); </code></pre> <p>Output</p> <pre><code>prefix dir1 dir2 dir3 dir4 </code></pre> <p><strong>Edit</strong></p> <p>Case with a <code>/</code> in the prefix, and we know what the prefix is:</p> <pre><code>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); </code></pre> <p>The substring without the prefix <code>"slash/prefix/"</code> is made by the <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#substring(int)" rel="noreferrer"><code>substring</code></a> method. That <code>String</code> is then run through <code>split</code>.</p> <p>Output:</p> <pre><code>dir1 dir2 dir3 dir4 </code></pre> <p><strong>Edit again</strong></p> <p>If this <code>String</code> is actually dealing with file paths, using the <a href="http://java.sun.com/javase/6/docs/api/java/io/File.html" rel="noreferrer"><code>File</code></a> class is probably more preferable than using string manipulations. Classes like <code>File</code> which already take into account all the intricacies of dealing with file paths is going to be more robust.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload