防止不需要的\ r \ n字符被添加到字符串中

时间:2014-06-13 20:17:58

标签: c# asp.net

我有一个填充了SQL调用的gridview。我尝试使用行中的一些单元格填充连接的字符串,然后将其转发到电子邮件正文中。我遇到的问题是,在添加单元格值时,每个单元格值(所有文本)都附加\r\n。我希望以类似于:

的格式传递字符串
"First Name, Last Name, Department..."
"First Name, Last Name, Department..."
etc.

我得到的是:

"\r\n
\First Name\r\n
Last Name \r\n
Department \r\n

我是否必须单独处理字符串以修剪不需要的字符,还是有更简单的方法?

foreach (GridViewRow row in PayrollGridView.Rows)
{
   HiddenField timecardIdHiddenField = (HiddenField)row.FindControl("TimecardIdHiddenField");
   int timecardId = Convert.ToInt32(timecardIdHiddenField.Value);

   tempString += Regex.Replace((row.Cells[0].Controls[0] as DataBoundLiteralControl).Text, "<.*?>", string.Empty) + ", ";
   tempString += Regex.Replace((row.Cells[1].Controls[0] as DataBoundLiteralControl).Text, "<.*?>", string.Empty) + ":  ";
   tempString += "Regular hours: " + Regex.Replace((row.Cells[2].Controls[0] as DataBoundLiteralControl).Text, "<.*?>", string.Empty) + ", ";
   tempString += "Overtime hours: " + Regex.Replace((row.Cells[3].Controls[0] as DataBoundLiteralControl).Text, "<.*?>", string.Empty) + ", ";

   RadioButtonList radioList = (RadioButtonList)row.FindControl("ActionRadioButton");

   if (radioList.SelectedItem.Text == "Approve")
   {
      tempString += "Approved." + "<br />";

      SqlHelperClass.ApproveTimecard(timecardId);
   }
   else if (radioList.SelectedItem.Text == "Disapprove")
   {
      approvalList += "DISAPPROVED." + "<br />";
   }

   tempString.Replace("\r\n", " ");
   approvalList += tempString + "<br />";
}

1 个答案:

答案 0 :(得分:1)

如果您使用StringBuilder附加每个单元格的内容,请确保使用Append而不是AppendLine。同样,如果您使用的是StringWriter,请使用Write而不是WriteLine。