Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to compare the first list element to second element of list and return 2nd element, third to fourth and and return fourth in c# language
such as
newList[i].Endswith(newList[i+1])
return newList[i+1]

but it is not working correctly. is there any the best solution to solve such problem
Posted

1 solution

Try:
C#
for (int i = 0; i < newList.Count - 1; i++)
   {
   if (newList[i].EndsWith(newList[i + 1]))
      return newList[i + 1];
   }



"This return only last matching condition.
if we have [0]read ,[1]ad and [2]salt, [3]alt it return only last 'alt' it also return 'ad' and 'alt'"


If that's what you want to do, then you have to explain it to us!
Remember that we can't see your screen, access your HDD, or read your mind.:laugh:

Try this:
C#
List<string>>matches = new List<string>();
for (int i = 0; i < newList.Count - 1; i++)
   {
   if (newList[i].EndsWith(newList[i + 1]))
      matches.Add(newList[i + 1]);
   }
The matches list contains all the "ad" and "alt" values.
 
Share this answer
 
v2
Comments
Member 11805607 22-Jul-15 5:44am    
This return only last matching condition.
if we have [0]read ,[1]ad and [2]salt, [3]alt it return only last 'alt' it also return 'ad' and 'alt'
OriginalGriff 22-Jul-15 5:59am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900