I want to match two string in the same list. I want to get words from a string and insert into list. I want to remove white space and separate by commas. Then I want to check two string in that list whether match or not.
Here is my code:
main() {
List<String> list = new List();
String str = "dog , dog , cat, tiger, lion, cat";
String strn = str.replaceAll(" " , "");
list = strn.split(",");
print(list.length);
print(list);
for (int i=0;i<list.length;i++){
if (list[i] == list[i+1]) {
print("same");
} else{
print("not same");
}
i++;
}
}
here string only check upto length 4. and white space not removed!
itwice (i++in the for header and in its body) and that you count one too far in the loop (if i islist.length - 1thenlist[i+1]will throw, you just happen to dodge this by incrementingitwice so it never hits the value 5).ianswers the first question (it actually doesn't hit indices 1, 3 and 5). Also, it does remove white-space. Instead ofprint(list);, try usingprint(list.join(","));. TheList.toStringmethod adds comma+space between elements.