Mujibur Rehman Ansari
Mujibur Rehman Ansari

Reputation: 663

split with file path getting wrong answer

Here is the code

fullPath = "\\some_path\som_more_path\test";
basePath = "\\some_path\som_more_path";

fullPath.split(basePath);

I am expecting an array with 2 elements first would be an empty string and second would be "\test" as this working perfectly in javascript

But instead of the expected output some how I am getting entire fullPath :(

Not able to figure it out what I am doing wrong here...

Upvotes: 0

Views: 77

Answers (1)

DuncG
DuncG

Reputation: 15186

As split expects a regex, you need to quote your path so it is read as the exact value of the path and not regex expression:

 fullPath.split(Pattern.quote(basePath));

This should return 2 element array, the same as new String[] { "", "\test" } as you have seen in Javascript.

Upvotes: 2

Related Questions