从字符串中删除2个数字

时间:2014-01-22 06:58:15

标签: java string

我正在阅读一份包含位置和坐标列表的.txt文件。

每一行都包含一个位置的名称,纬度和经度:

Downtown Flaggington 49.46419 -81.995353
Bender and Soonr 40.45519 -88.909353
...

如何从每行中删除最后两个数字才能得到名称?

这就是我所拥有的:

public class GPSFileReverser
{
    public static String reverse(Scanner in, String name, double lat, double lon) throws FileNotFoundException{

        in = new Scanner(new File("gps-and-place.txt"));

        GPSLocation[] gps = new GPSLocation[100];

        for(int i = 0; i < 100; i++) {
            name = in.nextLine();
            name= name.;
            lat = in.nextDouble();
            lon = in.nextDouble();
            GPSLocation currentGPS = new GPSLocation(name, lat, lon);
            gps[i] = currentGPS;


        }
        return name + " " + lat + " \t " + lon;
    }
}

1 个答案:

答案 0 :(得分:0)

使用正则表达式

        String s = "Bender and Soonr 40.45519 -88.909353";
        String pt = "^(.*) (-?\\d*\\.\\d*) (-?\\d*\\.\\d*)$";
        Pattern p = Pattern.compile(pt);
        Matcher m = p.matcher(s);
        if(m.find())
        System.out.println(m.group(1));