使用line.contains查找确切的字符串

时间:2017-08-22 18:30:00

标签: java

如果这些信息中的某些信息缺乏知识,那就开始学习Java了。

在此工作中,用户搜索道路和城镇。问题是当搜索像'Cabramatta'这样的东西时,结果'Cabramatta West'也会出现在结果中。

正在阅读的信息格式如下:

William Street ^ 3 ^ 3503 ^ Collins Street ^ Cabramatta West

William Street ^ 3 ^ 3503 ^ Collins Street ^ Cabramatta

public class TestGPS {

    public static void main(String[] args) {
        // Positions
        GPSPosition position = new GPSPosition(-37.2, 87.2, 200.0); 
        GPSPosition position2 = new GPSPosition(37.2, 7.2, 100.0);  

        // Set
        position.setAltitude(200.0); position.setLatitude(-37.2);
        position.setLongitude(87.2);

        // Get
        System.out.println("Altitude:  " + position.getAltutude()); 
        System.out.println("Latitude:  " + position.getLatitude()); 
        System.out.println("Longitude: " + position.getLongitute());

        // Compare to
        System.out.println("Comparison: " + position.compareTo(position2));

        // To String call
        System.out.println("Position: " + position.toString());

        // Distance between two positions
        System.out.println("Distance: " + position.distance(position2));   

    }

如何在搜索时找到'Cabramatta'的结果,还可以在搜索时找到'Cabramatta West'的结果?

3 个答案:

答案 0 :(得分:0)

之前拆分,然后只使用equals的{​​{1}}方法。这是一个测试代码:

String

输出:

        String line = "William Street^3^3503^Collins Street^Cabramatta West";
        String suburbInput = "Cabramatta";
        String roadInput = "Collins Street";

        String tramDetails[] = line.split("\\^");
        String crossStreet = tramDetails[0];
        String stopNumber = tramDetails[1];
        int stopNumberInt = Integer.parseInt(stopNumber);
        String trackerID = tramDetails[2];
        int trackerIDInt = Integer.parseInt(trackerID);
        String roadName = tramDetails[3];
        String suburbName = tramDetails[4];

        if (suburbInput.equals(suburbName) && roadInput.equals(roadName))
            System.out.print("'Suburb': " + suburbName + " 'Road': " + roadName + " 'Cross Street': " + crossStreet
                    + " 'Stop': " + stopNumberInt + " 'Tracker ID': " + trackerIDInt + "\n");

        suburbInput = "Cabramatta West";

        if (suburbInput.equals(suburbName) && roadInput.equals(roadName))
            System.out.print("'Suburb': " + suburbName + " 'Road': " + roadName + " 'Cross Street': " + crossStreet
                    + " 'Stop': " + stopNumberInt + " 'Tracker ID': " + trackerIDInt + "\n");

希望这有帮助!

答案 1 :(得分:0)

您可以在郊区使用String#endsWith()代替String#contains()

if (line.endsWith(suburbInput) && line.contains(roadInput))

当然,这只是一个创可贴。 '卡市'仍然会匹配西卡布拉马塔'问题是if (...)语句已实施,只能找到可能的匹配。您需要将该行解析为要匹配的确切字段,然后明确地测试这些字段。

或者(大锤方法),您可以实现一个正则表达匹配器,它将一次完全匹配所有内容。

答案 2 :(得分:0)

您必须在检查前拆分输入,以便使用.equals代替.contains。

while(fileName.hasNext())
 {
    String line =fileName.nextLine();
    String tramDetails[] = line.split("\\^");
    String suburbName = tramDetails[4];
    String roadName = tramDetails[3];

    if(suburbName.equals(suburbInput) && roadName.equals(roadInput))
    {

          String crossStreet = tramDetails[0];
          String stopNumber = tramDetails[1];
          int stopNumberInt = Integer.parseInt(stopNumber);
          String trackerID = tramDetails[2];
          int trackerIDInt = Integer.parseInt(trackerID);

          System.out.print("'Suburb': " + suburbName 
                            + " 'Road': " + roadName 
                            + " 'Cross Street': " + crossStreet   
                            + " 'Stop': " + stopNumberInt 
                            + " 'Tracker ID': " + trackerIDInt + "\n");
    }