remove chars from field in dataset

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

How do you remove all chars such as carriage return and new line from a
particular field for all records in a dataset?
 
Found the answer :

foreach(DataRow dr in ds.Tables["LeadDetails"].Rows)
{
dr["Activity"] = dr["Activity"].ToString().Replace("\r", "");
dr["Activity"] = dr["Activity"].ToString().Replace("\n", "");
dr["Activity"] = dr["Activity"].ToString().Replace("\t", "");
}
 
Mike said:
Found the answer :

foreach(DataRow dr in ds.Tables["LeadDetails"].Rows)
{
dr["Activity"] = dr["Activity"].ToString().Replace("\r", "");
dr["Activity"] = dr["Activity"].ToString().Replace("\n", "");
dr["Activity"] = dr["Activity"].ToString().Replace("\t", "");
}

You do a lot of indexing and converting for no reason. You don't have to
put the value back for each operation.

foreach(DataRow dr in ds.Tables["LeadDetails"].Rows) {
dr["Activity"] = ((string)dr["Activity"]).Replace("\r",
string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty);
}

or using the Regex class:

foreach(DataRow dr in ds.Tables["LeadDetails"].Rows) {
Regex.Replace((string)dr["Activity"], "[\\r\\n\\t]", string.Empty);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top