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!
dart
add a comment |
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!
dart
1
What is your question? I can see that you incrementi
twice (i++
in the for header and in its body) and that you count one too far in the loop (if i islist.length - 1
thenlist[i+1]
will throw, you just happen to dodge this by incrementingi
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 ofi
answers 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.toString
method adds comma+space between elements.
– lrn
Oct 18 at 13:47
add a comment |
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!
dart
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
dart
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 incrementi
twice (i++
in the for header and in its body) and that you count one too far in the loop (if i islist.length - 1
thenlist[i+1]
will throw, you just happen to dodge this by incrementingi
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 ofi
answers 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.toString
method adds comma+space between elements.
– lrn
Oct 18 at 13:47
add a comment |
1
What is your question? I can see that you incrementi
twice (i++
in the for header and in its body) and that you count one too far in the loop (if i islist.length - 1
thenlist[i+1]
will throw, you just happen to dodge this by incrementingi
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 ofi
answers 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.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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Oct 18 at 14:58
graphicbeacon
364
364
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 islist.length - 1
thenlist[i+1]
will throw, you just happen to dodge this by incrementingi
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 ofprint(list);
, try usingprint(list.join(","));
. TheList.toString
method adds comma+space between elements.– lrn
Oct 18 at 13:47