用逗号连接字符串以加入长字符串

时间:2013-07-05 10:11:37

标签: c# generics

我正在获得下面的课程列表。

 public class SmsResponse
    {
        public string AliasName { get; set; }
        public string CellPhoneNumber { get; set; }
        public int Response { get; set; }
    } 

我将此列表传递给函数,以检查响应字段是否具有0以外的响应,如果它有错误,我必须通过此方法PrepareStatusString();准备状态字符串。

bool isSuccess = EvaluateSmsResponse(responseList);  //list of smsresponse class

private bool EvaluateSmsResponse(List<SmsResponse> smsResponseList)
    {
        bool isSent = smsResponseList.Exists(response => response.Response != 0);
        if (!isSent)
            PrepareStatusString(smsResponseList);
        return isSent;
    }

     private void PrepareStatusString(List<SmsResponse> responseList)
    {
        bool isfirst = true;
        foreach (var item in responseList)
        {
            if (item.Response != 0)
            {
                if(isfirst)
                    StatusDescription += item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString();
                else
                    StatusDescription += "," + item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString();

                isfirst = false;
            }
        }
    }

代码按预期工作,但可以以任何方式进行优化/改进。我觉得有一个范围改进,但无法弄清楚??

4 个答案:

答案 0 :(得分:4)

StringBuilder在foreach循环中附加字符串会更有效(取决于迭代次数)

private void PrepareStatusString(List<SmsResponse> responseList)
{
    bool isfirst = true;
    StringBulder sb = new StringBuilder();
    foreach (var item in responseList)
    {
        if (item.Response != 0)
        {
            if(isfirst)
                sb.AppendFormat("{0}|{1}|{2}", item.AliasName, item.CellPhoneNumber,item.Response.ToString());
            else
                sb.AppendFormat(",{0}|{1}|{2}", item.AliasName, item.CellPhoneNumber, item.Response.ToString());

            isfirst = false;
        }
    }

    StatusDescription = sb.ToString();
}

答案 1 :(得分:4)

如果您使用的是.NET 4或更高版本,则可以覆盖SmsResponse.ToString(),然后使用String.Join<T>(String, IEnumerable<T>)来连接响应。

因此,您的SmsResponse课程可能如下所示:

public class SmsResponse
{
    public string AliasName { get; set; }
    public string CellPhoneNumber { get; set; }
    public int Response { get; set; }

    public override string ToString()
    {
        return AliasName + "|" + CellPhoneNumber + "|" +
            Response.ToString();
    }
}

PrepareStatusString将是:

private void PrepareStatusString(List<SmsResponse> responseList)
{
    StatusDescription = string.Join(",", responseList.Where(i => i.Response != 0));
}

答案 2 :(得分:1)

我不知道优化,但可以更明确地重写如下:

private void PrepareStatusString(List<SmsResponse> responseList)
{
    StatusDescription = responseList
         .Where(x => x.Response != 0)
         .Select(x => x.AliasName 
                    + "|" + x.CellPhoneNumber 
                    + "|" + x.Response.ToString())
         .Aggregate((x, y) => x + "," + y);
}

请注意,如果您预计会有超过几百个对象,StringBuilder只会提供明显的性能优势。

答案 3 :(得分:0)

像这样使用string.Join

    List<string> elements = new List<string>();
    foreach (var item in responseList)
    {
        if (item.Response != 0)
        {
            elements.add(item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString());
        }
    }
    string result = string.Join(",", elements.ToArray());