【问题标题】:Why StartsWith is returning me a wrong item为什么 StartsWith 退回给我一个错误的项目
【发布时间】:2018-03-28 14:08:11
【问题描述】:

我正在使用String.startsWith 方法比较两个字符串,看看其中一个是否以引入的字符开头。

代码

List<Province> models;

public int getIndexIfStartsWith(String str){

    for(int index = 0; index < models.size(); index++){
        if(str.startsWith(models.get(index).getKey())){
            return index;
        }
    }
    return -1;
}

我正在与西班牙的省份列表进行比较。如果我将14006 作为一个字符串进行比较,该方法将使用key(诸如“1”、“28”等数字)和value(省份名称)对省份列表进行迭代。

当我输入14006 时,该方法返回我的项目"1", "ALAVA",但“1”与“14”不同,后者是比较值的乞求。密钥长度为 1 的 ALAVA,不应与“14”(所需值的乞求)进行比较。

怎么了??

compare 14006 against 1, ALAVA
                      2, ALBACETE
                      3, ALICANTE
                      ...
                      14, CORDOBA

Córdoba, 14006 应该是唯一的匹配项,但 Alava 也会返回。

我用它来用零填充数字,所以我可以每次都与 2 位数字进行比较。

public static String padLeft(String stringToPad, int padToLength){
    String retValue = null;
    if(stringToPad.length() < padToLength) {
        retValue = String.format("%0" + String.valueOf(padToLength - stringToPad.length()) + "d%s",0,stringToPad);
    }
    else{
        retValue = stringToPad;
    }
    return retValue;
  }

后来,我用这个...

public int getIndexIfStartsWith(String str){

    for(int index = 0; index < models.size(); index++){
        if(str.startsWith(CommonUtils.padLeft(models.get(index).getKey(), 2))){
            return index;
        }
    }
    return -1;
}

【问题讨论】:

  • 什么是模型类型?什么清单?
  • Arraylist list ---> 字符串键,字符串值。密钥存储一个数字。我需要和这个比较

标签: android string compare match


【解决方案1】:

您的 if 方法是错误的。当它在第一个省份"1", "ALAVA" 上测试时,您正在测试14006 starts with 1 是否为真,当您要测试1 starts with 14006 为假时,只有当您到达14006 键时才会通过。

应该是

  if(models.get(index).getKey().startsWith(str)) { 
     ...
  }

注意

你的方法只返回第一个匹配,如果你通过了可能会有几个匹配,比如说"2",也许你应该返回一个列表。

但如果你想要完美匹配,你应该使用equals()

编辑(新需求定义后)

鉴于以下省份列表:

List<Province> models = new ArrayList<>();
models.add(new Province("1", "ALAVA"));
models.add(new Province("2", "ALBACETE"));
models.add(new Province("3", "ALICANTE"));
models.add(new Province("14", "CORDOBA"));

您可以通过以下方法返回匹配索引

public static int getIndexIfStartsWith(String str){

  int matchingIndex = -1;

  // make sure we have at least 2 digits (left pad if needed)
  if(str.length() == 1) {
    str = "0"+str;
  }

  String first2Digits = str.substring(0, 2);

  for(int i=0; i<models.size(); i++) {
    String provinceKey = models.get(i).getKey();
    // make sure the province key also has at least 2 digits (left pad if needed)
    if(provinceKey.length() == 1) {
      provinceKey = "0" + provinceKey;
    }

    if(provinceKey.startsWith(first2Digits))
      matchingIndex = i;
  }
  return matchingIndex;
}

这将为“1”和“01”返回 ALAVA,为“14”和“14006”返回 CORDOBA

【讨论】:

  • 那么,有了这个改变,当比较 14 和 14006m 时,结果又会是假的,不是吗?
  • 否,因为您将测试 if(14 starts with 14006) 这也是错误的。更改它,看看它是否符合您的要求。如果您想要完美匹配,请使用 .equals 而不是 .startsWith
  • 我不需要完美匹配,但我只需要比较字符串的前2个字符(如果输入14006,我将只使用14进行比较),那么在这种情况下1 不应该是结果,只有 14 的项目。
  • ok 所以你的要求是,如果你输入14006 ,你返回所有key以14开头的省份?并且可以有几个。对吗?
  • 我将您的编辑添加到您的问题中。并修改了我的答案,试试看现在是否有效
猜你喜欢
  • 2018-10-09
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 2017-12-17
  • 2013-11-14
  • 1970-01-01
相关资源
最近更新 更多