使用“foreach”或“for”Loop构造从数组中构造字符串

时间:2013-01-09 07:13:52

标签: c# asp.net-mvc-3

我需要使用for循环,我想我可以访问数组中每行的每个属性来构成我的字符串。

  public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString)
  {

    foreach (var part in HintTextString)
    {
      HintText = HintText + "  " + part.Name + "  " + part.IncentiveTrainingModuleOptionName + " = " + part.Points;
    }

    return HintText;
  }

  public class UserTrainingPointsDataModel
  {
    public virtual int InteractionType { get; set; }
    public virtual int Points { get; set; }
    public virtual string Name { get; set; }
    public virtual string IncentiveTrainingModuleOptionName { get; set; }
  }

每一行都有值:

  China Incentive Program  Points For Completing = 50  
  India - Q2 Incentive   Points Per Correct Answer = 50  
  China - Q2 Incentive  Points For Completing = 50  
  India Incentive Program  Points Per Correct Answer = 100  
  India - Q2 Incentive   Points Per Correct Answer = 100

HintTextString包含5行,每行包含上面的属性,我需要组成一个如下所示的字符串:

  "Incentive Program: " + part.Name[0] + "  " + part.IncentiveTrainingModuleOptionName[0] + " + " + part.Points[0] + 
  "Incentive Program: " + part.Name[1] + "  " + part.IncentiveTrainingModuleOptionName[1] + " + " + part.Points[1]

等。所以我需要一个for循环呢?以及如何获得HintTextString的长度? intellisense不给我那个属性

观点:

 <% if (module.HasAssessment)
 { %>
    <div class="<%: moduleStateClass %>">&nbsp;</div>
    <div class="<%: moduleScoreClass %>"><%: module.ModuleScore %></div>
    <% var HintText =  TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %>

    <script type="text/javascript">
       <%: Html.GetQTip("training-module-id-" + module.TrainingModuleId , "Incentive program: " + HintText , "training-module-id-" + module.TrainingModuleId , Zinc.Web.Extensions.QTipPosition.Bottom, true, "Module Points") %>
    </script> 
 <% }  %> 
 <% else
    {  %>
 <% var HintText =  TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %>
 <script type="text/javascript">
    <%: Html.GetQTip("training-module-id-" + module.TrainingModuleId , "Incentive program: "  + HintText ,"training-module-id-" + module.TrainingModuleId , Zinc.Web.Extensions.QTipPosition.Bottom, true, "Module Points") %>   
 </script>
 <% } %>    

“行动”:

  <div class="module-column-content">
    <% Html.RenderPartial("~/Areas/Training/Views/Home/Details.ascx", Model); %>
  </div>

由于

2 个答案:

答案 0 :(得分:1)

如果您只想将整个列表以您描述的格式返回为一个,这应该可行

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString)
{
    string returnValue = string.Empty;
    foreach (var dataModel in HintTextString)
    {
        returnValue += string.Format("Incentive Program: {0}  {1} + {2}  ", dataModel.Name, dataModel.IncentiveTrainingModuleOptionName, dataModel.Points);
    }
    return returnValue;
}

正如 Jared 所提到的,如果你的收藏很大,你可能想要使用StringBuilder,因为它在性能方面要好得多。

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString)
{
    var returnValue = new StringBuilder();
    foreach (var dataModel in HintTextString)
    {
        returnValue.AppendFormat("Incentive Program: {0}  {1} + {2}  ", dataModel.Name, dataModel.IncentiveTrainingModuleOptionName, dataModel.Points);
    }
    return returnValue.ToString();
}

答案 1 :(得分:0)

使用StringBuilder构建文本,同时迭代集合。它经过优化,可以处理字符串操作功能。如果在循环中使用简单文本添加(+=),性能可能会变得很糟糕。

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString)
{
  //format for each item 
  //{Name} {ModuleName} = {Points} <NewLine>
  string lineFormat = "Incentive Program: {0} {1} = {2}\r\n";
  StringBuilder sb = new StringBuilder();
  foreach (var part in HintTextString)
  {
     sb.AppendFormat(lineFormat, part.Name, part.IncentiveTrainingModuleOptionName, part.Points);
  }

  return sb.ToString();
}

此外,如果您需要HintTextString集合的长度,则可以使用IEnumerable.Count()扩展方法。