Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code like as:

C#
List<String> listA = new List<string> { "ramesh", "rammi", "ramu","ramarao","raja","rajesh"};

foreach(string s in listA)
{

}


this is my code. in this i have list string collection.i know how to compare two

strings but in this situation(foreach) how can u compare each string to another

string.i want to how can u compare each string to another string using foreachloop.

please help me.
thank u
Posted
Comments
F-ES Sitecore 24-Mar-15 6:08am    
What do you mean by compare each string to each other? What is it you're trying to achieve?
Krishna Veni 24-Mar-15 6:29am    
ya.compare each string to each other
Matt T Heffron 25-Mar-15 20:06pm    
As F-ES Sitecore said: What do you mean by "compare"?
Check for equality?
Check for some kind of similarity?
??

Tell us what you're trying to accomplish and maybe we can help.

1 solution

I would go for two for loops instead of one foreach:
C#
List<String> listA = new List<string> { "ramesh", "rammi", "ramu", "ramarao", "raja", "rajesh" };
 
for (int i = 0; i < listA.Count - 1; i++)
{
   for (int j = i + 1; j < listA.Count; j++)
   {
      // Here you can compare listA[i] and listA[j]
   }
}
 
Share this answer
 

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