1
votes

Comparer plusieurs chaînes Android

ce que j'essaie de faire est de vérifier si plusieurs chaînes sont nulles pour passer à l'activité suivante, quelque chose comme ceci:

if( string1.equal(null) && ... stringN.equal(null))
{ Toast.makeText(this, "something is empty", Toast.LENGTH_SHORT).show();}
else
{ GO TO THE NEXT ACTIVITY}

toutes les chaînes doivent avoir un contenu. . Toute aide serait formidable


0 commentaires

4 Réponses :


0
votes

Si vos chaînes sont dans une liste ou un tableau, vous pouvez les itérer.

Si vous utilisez une liste Array:

private boolean verify(final String[] list)
{
    for (String entry : list)
    {
        if (TextUtils.isEmpty(entry))
            return false;
    }

    return true;
}

Ou si vous utilisez un tableau, même chose vient de changer le paramètre en String []:

private boolean verify(final ArrayList<String> list)
{
    for (String entry : list)
    {
        if (TextUtils.isEmpty(entry))
            return false;
    }

    return true;
}


0 commentaires

0
votes

Vous pouvez également le faire comme ceci:

private String[] array;

private String one = "one";
private String two = "two";
private String three = "three";
private String four = "four";
private String five = "five";


private void someMethod(){
    if(isStringsNotNull(getStringArray())){
        //Start another activity
    } else {
        //Do something
    }
}

private String[] getStringArray(){
    return new String[]{one, two, three, four, five};
}

private Boolean isStringsNotNull(String[] array){
    for(String str: array){
        if(str == null){
            return false;
        }
    }
    return true;
}

Bonne réponse @PerracoLabs, mais isEmpty vérifie uniquement si la longueur est égale à 0. N'est-ce pas?


0 commentaires

0
votes

si vous utilisez java8 / lambda, vous pouvez essayer d'utiliser cette fonction:

List<String> stringList = new ArrayList<>(Arrays.asList(string2.split(" "))); // Split the string into list
        // compare the list with the source of the string, if match put into the list of result
        List<String> result = stringList.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
//                return string1.contains(s); // case sensitive
                return Pattern.compile(Pattern.quote(s), Pattern.CASE_INSENSITIVE).matcher(string1).find(); // case insensitive
            }
        }).collect(Collectors.toList());

        // check the match results
        if(result.size()<stringList.size()){
            Log.d("test", "onCreate: not match");
        }else{
            Log.d("test", "onCreate: match");
        }


0 commentaires

0
votes

Si vous avez un nombre variable de chaînes à vérifier, je ferais quelque chose comme ceci:

if (validate(string1, string2, stringN)) {
  Toast.makeText(this, "something is empty", Toast.LENGTH_SHORT).show();
} else { 
  // GO TO THE NEXT ACTIVITY
}

Ensuite, votre code appellerait la méthode validate avec autant de chaînes que nécessaire pour valider :

private boolean validate(String... items) {
  for (String item : items) {
    if (TextUtils.isEmpty(item)) {
      return false;
    }
  }
  return true;
}


0 commentaires