java.util.scanner - Java scanner multiple/several delimiter conditions -
i have problem in finding right way city1, city 2 , distance without "km", using scanner , delimiter. need text in bold.
string (example):
city1, country; city2, country; 145 km
my delimitar far because have no idea:
scanner.usedelimiter(";");
i tried using string.split("") without success.
should first devide in 3 parts on ";" , new delimitar each part? or there easier way?
thanks in advance
(edit explain difference post: tried regex, no luck. need split string on multiple places on different characters. wouldn't ask question if i'm able solve post.)
you can use combination of scanner
, string#split
:
string source = "city1, country; city2, country; 145 km"; scanner scanner = new scanner(source); scanner.usedelimiter(";"); while (scanner.hasnext()) { // trim() removes leading , trailing whitespace string line = scanner.next().trim(); // split line on either ',' or space string[] split = line.split(",|\\s"); // print first word system.out.println(split[0]); }
Comments
Post a Comment