

To learn more about how substring works, visit Java String substring().Įxample 3: Get a relative path from two absolute paths using package import That is, the string represented by absolutePath2 is removed from absolutePath1. Here, the substring() method returns the part of absolutePath1 starting from index equal to the length of absolutePath2.

Notice the expression, absolutePath1.substring(absolutePath2.length())

In the above example, we have converted the file paths to strings. String relativePath = absolutePath1.substring(absolutePath2.length())
