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

本文关键字:添加 字符串 字符 不需要 | 更新日期: 2023-09-27 18:08:46

我有一个用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 />";
}

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

如果您使用StringBuilder来附加每个单元格的内容,请确保使用append而不是AppendLine。类似地,如果您正在使用StringWriter,请使用Write而不是WriteLine。