Compare string in list











up vote
1
down vote

favorite












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!










share|improve this question




















  • 1




    What is your question? I can see that you increment i twice (i++ in the for header and in its body) and that you count one too far in the loop (if i is list.length - 1 then list[i+1] will throw, you just happen to dodge this by incrementing i twice so it never hits the value 5).
    – lrn
    Oct 18 at 11:30










  • My question why it doesn't hit index 4 and 5? and why it doesn't remove white space?
    – Falak
    Oct 18 at 11:40










  • The double increment of i answers the first question (it actually doesn't hit indices 1, 3 and 5). Also, it does remove white-space. Instead of print(list);, try using print(list.join(","));. The List.toString method adds comma+space between elements.
    – lrn
    Oct 18 at 13:47

















up vote
1
down vote

favorite












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!










share|improve this question




















  • 1




    What is your question? I can see that you increment i twice (i++ in the for header and in its body) and that you count one too far in the loop (if i is list.length - 1 then list[i+1] will throw, you just happen to dodge this by incrementing i twice so it never hits the value 5).
    – lrn
    Oct 18 at 11:30










  • My question why it doesn't hit index 4 and 5? and why it doesn't remove white space?
    – Falak
    Oct 18 at 11:40










  • The double increment of i answers the first question (it actually doesn't hit indices 1, 3 and 5). Also, it does remove white-space. Instead of print(list);, try using print(list.join(","));. The List.toString method adds comma+space between elements.
    – lrn
    Oct 18 at 13:47















up vote
1
down vote

favorite









up vote
1
down vote

favorite











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!










share|improve this question















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!







dart






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 18:26









julemand101

10117




10117










asked Oct 18 at 11:19









Falak

153




153








  • 1




    What is your question? I can see that you increment i twice (i++ in the for header and in its body) and that you count one too far in the loop (if i is list.length - 1 then list[i+1] will throw, you just happen to dodge this by incrementing i twice so it never hits the value 5).
    – lrn
    Oct 18 at 11:30










  • My question why it doesn't hit index 4 and 5? and why it doesn't remove white space?
    – Falak
    Oct 18 at 11:40










  • The double increment of i answers the first question (it actually doesn't hit indices 1, 3 and 5). Also, it does remove white-space. Instead of print(list);, try using print(list.join(","));. The List.toString method adds comma+space between elements.
    – lrn
    Oct 18 at 13:47
















  • 1




    What is your question? I can see that you increment i twice (i++ in the for header and in its body) and that you count one too far in the loop (if i is list.length - 1 then list[i+1] will throw, you just happen to dodge this by incrementing i twice so it never hits the value 5).
    – lrn
    Oct 18 at 11:30










  • My question why it doesn't hit index 4 and 5? and why it doesn't remove white space?
    – Falak
    Oct 18 at 11:40










  • The double increment of i answers the first question (it actually doesn't hit indices 1, 3 and 5). Also, it does remove white-space. Instead of print(list);, try using print(list.join(","));. The List.toString method adds comma+space between elements.
    – lrn
    Oct 18 at 13:47










1




1




What is your question? I can see that you increment i twice (i++ in the for header and in its body) and that you count one too far in the loop (if i is list.length - 1 then list[i+1] will throw, you just happen to dodge this by incrementing i twice so it never hits the value 5).
– lrn
Oct 18 at 11:30




What is your question? I can see that you increment i twice (i++ in the for header and in its body) and that you count one too far in the loop (if i is list.length - 1 then list[i+1] will throw, you just happen to dodge this by incrementing i twice so it never hits the value 5).
– lrn
Oct 18 at 11:30












My question why it doesn't hit index 4 and 5? and why it doesn't remove white space?
– Falak
Oct 18 at 11:40




My question why it doesn't hit index 4 and 5? and why it doesn't remove white space?
– Falak
Oct 18 at 11:40












The double increment of i answers the first question (it actually doesn't hit indices 1, 3 and 5). Also, it does remove white-space. Instead of print(list);, try using print(list.join(","));. The List.toString method adds comma+space between elements.
– lrn
Oct 18 at 13:47






The double increment of i answers the first question (it actually doesn't hit indices 1, 3 and 5). Also, it does remove white-space. Instead of print(list);, try using print(list.join(","));. The List.toString method adds comma+space between elements.
– lrn
Oct 18 at 13:47














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I also noticed that in the for loop you are incrementing i twice, the second being close to the bottom. This causes i to skip some of the indexes, so loop looks at index 0, then 2, then 4, then it stops.



I have refactored your solution slightly. I removed the second i++ and changed i < list.length to i < list.length - 1 to skip the last item as list[i + 1] will throw an out of range exception:



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.join('|'));

for(int i=0; i < list.length - 1; i++){
if(list[i] == list[i+1]){
print("same");
}
else{
print("not same");
}
}
}


The result of the loop is so:



same
not same
not same
not same
not same


You can test this out on DartPad






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52872822%2fcompare-string-in-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    I also noticed that in the for loop you are incrementing i twice, the second being close to the bottom. This causes i to skip some of the indexes, so loop looks at index 0, then 2, then 4, then it stops.



    I have refactored your solution slightly. I removed the second i++ and changed i < list.length to i < list.length - 1 to skip the last item as list[i + 1] will throw an out of range exception:



    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.join('|'));

    for(int i=0; i < list.length - 1; i++){
    if(list[i] == list[i+1]){
    print("same");
    }
    else{
    print("not same");
    }
    }
    }


    The result of the loop is so:



    same
    not same
    not same
    not same
    not same


    You can test this out on DartPad






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      I also noticed that in the for loop you are incrementing i twice, the second being close to the bottom. This causes i to skip some of the indexes, so loop looks at index 0, then 2, then 4, then it stops.



      I have refactored your solution slightly. I removed the second i++ and changed i < list.length to i < list.length - 1 to skip the last item as list[i + 1] will throw an out of range exception:



      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.join('|'));

      for(int i=0; i < list.length - 1; i++){
      if(list[i] == list[i+1]){
      print("same");
      }
      else{
      print("not same");
      }
      }
      }


      The result of the loop is so:



      same
      not same
      not same
      not same
      not same


      You can test this out on DartPad






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        I also noticed that in the for loop you are incrementing i twice, the second being close to the bottom. This causes i to skip some of the indexes, so loop looks at index 0, then 2, then 4, then it stops.



        I have refactored your solution slightly. I removed the second i++ and changed i < list.length to i < list.length - 1 to skip the last item as list[i + 1] will throw an out of range exception:



        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.join('|'));

        for(int i=0; i < list.length - 1; i++){
        if(list[i] == list[i+1]){
        print("same");
        }
        else{
        print("not same");
        }
        }
        }


        The result of the loop is so:



        same
        not same
        not same
        not same
        not same


        You can test this out on DartPad






        share|improve this answer












        I also noticed that in the for loop you are incrementing i twice, the second being close to the bottom. This causes i to skip some of the indexes, so loop looks at index 0, then 2, then 4, then it stops.



        I have refactored your solution slightly. I removed the second i++ and changed i < list.length to i < list.length - 1 to skip the last item as list[i + 1] will throw an out of range exception:



        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.join('|'));

        for(int i=0; i < list.length - 1; i++){
        if(list[i] == list[i+1]){
        print("same");
        }
        else{
        print("not same");
        }
        }
        }


        The result of the loop is so:



        same
        not same
        not same
        not same
        not same


        You can test this out on DartPad







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 18 at 14:58









        graphicbeacon

        364




        364






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52872822%2fcompare-string-in-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()