Wandang
Wandang

Reputation: 952

why doesn't String.contains() return true in this case?

System.out.println(ganzeZeile[26]);
System.out.println(filter.get(11));
System.out.println(ganzeZeile[26].contains(filter.get(11)));

ganzeZeile is an array of Strings.
filter is an ArrayList of Strings.

ganzeZeile[26] = "Ich gebe der Dozentin/dem Dozenten die Gesamtnote."
filter.get(11) = "dem Dozenten die Gesamtnote"

But ganzeZeile[26].contains(filter.get(11)) returns false.

Isn't "dem Dozenten die Gesamtnote" part of "Ich gebe der Dozentin/dem Dozenten die Gesamtnote.", and therefore contains(...) should return true?

edit:

i've got my code and the testcode from assylias in a testclass, both return different values(!). i dont see any difference in code tbh.

import java.util.ArrayList;
import java.util.List;

public class test1 {

public static void main(String[] args) {
    String[] ganzeZeile = new String[28];
    ArrayList<String> filter = new ArrayList<String>();

    ganzeZeile[26] = "Ich gebe der Dozentin/dem Dozenten die Gesamtnote.";
    for (int i = 0; i < 11; i++) {
        filter.add("");
    }
    filter.add("dem Dozenten die Gesamtnote");

    System.out.println(ganzeZeile[26]);
    System.out.println(filter.get(11));
    System.out.println(ganzeZeile[26].contains(filter.get(11)));//returns false
}

//  public static void main(String[] args) {
//      String[] ganzeZeile = new String[28];
//      ArrayList<String> filter = new ArrayList<String>();
//
//      ganzeZeile[26] = "Ich gebe der Dozentin/dem Dozenten die Gesamtnote.";
//      for (int i = 0; i < 11; i++) {
//          filter.add("");
//      }
//      filter.add("dem Dozenten die Gesamtnote");
//
//      System.out.println(ganzeZeile[26]);
//      System.out.println(filter.get(11));
//      System.out.println(ganzeZeile[26].contains(filter.get(11))); //prints true
//  }
}

since i use the newest javaversion (1.7 atm) it could be the reason this code behaves so different.

regards

Upvotes: 0

Views: 2025

Answers (1)

assylias
assylias

Reputation: 328568

I can't reproduce the behavior based on the information you gave - the problem is probably somewhere else:

public static void main(String[] args) throws InterruptedException {
    String[] ganzeZeile = new String[27];
    List<String> filter = new ArrayList<String>();

    ganzeZeile[26] = "Ich gebe der Dozentin/dem Dozenten die Gesamtnote.";
    for (int i = 0; i < 11; i++) {
        filter.add("");
    }
    filter.add("dem Dozenten die Gesamtnote");

    System.out.println(ganzeZeile[26]);
    System.out.println(filter.get(11));
    System.out.println(ganzeZeile[26].contains(filter.get(11))); //prints true
}

Upvotes: 3

Related Questions