如何使用foreach循环比较c#中的字符串集合 [英] how can u compare string collection in c# using foreach loop

查看:97
本文介绍了如何使用foreach循环比较c#中的字符串集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如:



 List< String> listA =  new 列表< string> {  ramesh  rammi  ramu  ramarao  raja  rajesh}; 

foreach string s listA)
{

}





这是我的代码。在这个我有列表字符串collection.i知道如何比较两个



字符串,但在这种情况下(foreach)你怎么能比较每个字符串到另一个



string.i想要如何使用foreachloop将每个字符串与另一个字符串进行比较。



请帮助我。

谢谢你

解决方案

我会为循环代替一个 code> foreach

 List< String> listA =  new 列表< 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 ++)
{
// 在这里你可以比较listA [i]和listA [j]
}
}


my code like as:

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

解决方案

I would go for two for loops instead of one foreach:

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]
   }
}


这篇关于如何使用foreach循环比较c#中的字符串集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆