Reputation: 2280
I have a string that looks like this "./path1/path2/path3/path4/etc/file"
I would like to just remove the first 2 paths and return a string that looks like this:
"path3/path4/etc/file"
How can I do this using Scala?
Upvotes: 1
Views: 559
Reputation: 30756
If you're dealing with filesystem paths, it might be wise to use Java's Path
class that's designed for this sort of thing.
scala> import java.nio.file.Paths
import java.nio.file.Paths
scala> val p = Paths.get("./path1/path2/path3/path4/etc/file")
p: java.nio.file.Path = ./path1/path2/path3/path4/etc/file
scala> p.subpath(3, p.getNameCount())
res0: java.nio.file.Path = path3/path4/etc/file
Upvotes: 7
Reputation: 2618
How about simple: s.split("/").drop(3).mkString("/")
In this statement firstly you split path by /
, next remove first 3 tokens (as first one will be .
in your case) and finally merge tokens together to create new path.
Upvotes: 10