Skip to content
Advertisement

Substring a string in JAVA

Is possible to substring this String = "D:/test/for test/change.txt:D:/test/for test/further.txt:D:/test/for test/yandex.txt" to:

D:/test/for test/change.txt

D:/test/for test/further.txt

D:/test/for test/yandex.txt

Because are two column, I can not split() use ":".

Advertisement

Answer

A simple regular expression below splits on “:” that are followed by a “driveletter:”

String s = "D:/test/for test/change.txt:D:/test/for test/further.txt:D:/test/for test/yandex.txt";
s.split(":(?=\w:)");
==> String[3] { "D:/test/for test/change.txt"
              , "D:/test/for test/further.txt"
              , "D:/test/for test/yandex.txt" }

Note that this won’t help if additional paths don’t begin with driveletter:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement