如何使用输出特定形式的foreach循环。 [英] How do I use a foreach loop that output is in particular form.

查看:83
本文介绍了如何使用输出特定形式的foreach循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些ID输入到字符串中,如下所示:



'1','2','3'



我使用以下方式,但没有给出适当的结果。

  foreach (DataRow drCont  in  dt.Rows)
{
sOptInContactsIDs = ' + sOptInContactsIDs + ' + drCont [ ContactID] + ',;
}

sOptInContactsIDs = sOptInContactsIDs.Replace( '', );
int icount = sOptInContactsIDs.Length;
sOptInContactsIDs = sOptInContactsIDs.Remove(icount - 1 );





我缺少一些逻辑..... Plz帮助我。

谢谢。

解决方案

试试这个:

  string  sep =  ; 
StringBuilder sb = new StringBuilder();
foreach (DataRow drCont in dt.Rows)
{
sb.AppendFormat( {0}'{1}',sep,drCont [ ContactID]);
sep = ;
}
sOptInContactsIDs = sb.ToString();


你好,

在C#。我会建议您使用String.To计算长度简单使用溢出功能。

声明

<前lang =cs> string sOptInContactsIDs = string .Empty; ;



首先检查该列是否有值。



  foreach (DataRow drCont  in  dt.Rows)
{
< span class =code-keyword> if (drCont [ ContactID]! = DBNull.Value)
{
if (sOptInContactsIDs == string .Empty )
sOptInContactsIDs = ' + drCont [ ContactID] + ' ;
else
sOptInContactsIDs = sOptInContactsIDs + + ' + drCont [ ContactID] + ';
}
} // 每个循环的结束
< span class =code-comment> //
For Spilting It
int icount = sOptInContactsIDs.Split(' ,');



最初获取第一行时的情况价值是1

然后

sOptInContactsIDs ='+ 1 +'='1'

第二次假设价值是2

sOptInContactsIDs ='1'+,+'+ 2 +'='1','2'





如果你知道string.Format使用那个更好的执行。

如果可能用于循环而不是ForEach。



我在我的项目中使用了该代码。我收集了Array中的所有ID。然后使用For循环我收集了字符串中的值并在此之后插入逗号。



希望它能帮助你。



快乐编码: - )


I am trying to fetch some ids in to the string in form as follows:

'1','2','3'

I used the following way but its not giving the appropriate result.

foreach (DataRow drCont in dt.Rows)
       {
           sOptInContactsIDs = "'" + sOptInContactsIDs + "'"+  drCont["ContactID"] + "',";
       }

       sOptInContactsIDs = sOptInContactsIDs.Replace("'',", "");
       int icount = sOptInContactsIDs.Length;
       sOptInContactsIDs = sOptInContactsIDs.Remove(icount - 1);



I am missing some logic.....Plz help me on this.
Thanks.

解决方案

Try this:

string sep = "";
StringBuilder sb = new StringBuilder();
foreach (DataRow drCont in dt.Rows)
   {
   sb.AppendFormat("{0}'{1}'", sep, drCont["ContactID"]);
   sep = ",";
   }
sOptInContactsIDs = sb.ToString();


Hello,
In C# .I will Suggest You To Use String.To Count the length Simply Use Spilt Function.
Declare

string sOptInContactsIDs   = string.Empty; ;


First Check whether that Column Has value or not.

     foreach (DataRow drCont in dt.Rows)
       {
             if(drCont["ContactID"]!=DBNull.Value)
                 {
                      if(sOptInContactsIDs ==  string.Empty)
                          sOptInContactsIDs = "'"+ drCont["ContactID"] +"'";
                       else
                           sOptInContactsIDs = sOptInContactsIDs +","  +"'"+ drCont["ContactID"] +"'";
                  }
   }//Close of for each loop
//For Spilting It 
         int icount = sOptInContactsIDs.Split(',');


Intially When First Row is Fetched & Value Is 1
then
sOptInContactsIDs="'"+1+"'"= '1'
For Second time assume value is 2
sOptInContactsIDs = '1'+","+"'"+2+"'"='1','2'


If you are aware of string.Format Use that One For Better Exection.
If Possible Use For Loop Instead of ForEach.

I have Used that Code in My Project . i have collected all Ids in Array. Then Using For Loop I have collected the value in string and inserted comma after that.

Hope It Helps You.

Happy Coding :-)


这篇关于如何使用输出特定形式的foreach循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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