【发布时间】:2018-04-05 01:34:36
【问题描述】:
我得到一个包含“\”的字符串,但我想删除它。 只需使用
string.Replace(@"\", "");
不起作用。 这是字符串:
'Samsung Monitor 21\" '
应该是:
'Samsung Monitor 21" '
好的,那么让我详细介绍一下。
我正在使用 postgresql 数据库。我的测试数据库包含一些产品,例如。 -> 三星显示器 21"
"UPDATE artikel SET warengruppe = 5 WHERE artikel_nr = '1100pplus' AND
bezeichnung = 'Samsung Monitor 21\" ' AND lieferanten_nr = 134 AND
mengeneinheit = 'ST' AND verkaufspreis = '775.00' AND einkaufspreis
= '465.00' AND lieferzeit = 28 AND bestand_lager = 2 AND bestand_minimum =
10 AND jahresumsatz = '1550.00' AND vorjahresumsatz = '2325.00'"
由于每个值都是从数据网格单元格中取出的,它会自动将反斜杠放在 -> "
但只是按原样给出该字符串,它显然不起作用,因为我的数据库有
'Samsung Monitor 21" '
而不是
'Samsung Monitor 21\" '
仅使用“LIKE”是一种解决方法..
对于那些问的人,这是我的完整方法:
private void SQLDatagrid_OnCurrentCellChanged(object sender, DataGridCellEditEndingEventArgs e) {
DataGridColumn col1 = e.Column;
DataGridRow row1 = e.Row;
int row_index = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
int col_index = col1.DisplayIndex;
int i = 0;
string commandstring = "UPDATE artikel SET ";
string tmpcell = dt.Rows[row_index][col_index].ToString();
tmpcell = tmpcell.Replace(',', '.');
bool valueisint = isInteger(tmpcell);
commandstring += dt.Columns[col_index].ColumnName + " = ";
if (!valueisint)
{
commandstring += "'";
}
commandstring += tmpcell;
if (!valueisint)
{
commandstring += "'";
}
commandstring += " WHERE ";
foreach (DataColumn dataColumn in dt.Columns)
{
string cell = dt.Rows[row_index][i].ToString();
cell = cell.Replace(',', '.');
bool tmp = isInteger(cell);
if (i > 0 && i != col_index)
{
commandstring += " AND ";
}
if (i != col_index)
{
commandstring += dataColumn.ColumnName + " = ";
if (!tmp)
{
commandstring += "'";
}
commandstring += cell;
if (!tmp)
{
commandstring += "'";
}
}
i++;
}
commandstring = Regex.Replace(commandstring, @"\\", ""); // this is just trying out
commandstring = commandstring.Replace(@"/", ""); // As is this
SqlCommand command = new SqlCommand(commandstring);
【问题讨论】:
-
string str = "'Samsung Monitor 21\\\"'"; str = str.Replace("\\", ""); -
只需添加一个反斜杠即可转义 (
\`):string.Replace("\\", "");`. -
我敢打赌你没有
\的字符串。你有一个带有 " 的字符串 -
是我一个人还是这个问题每周至少出现一次?
-
我不明白为什么你们投反对票。该字符串是由程序生成的,所以我无法编辑它。而ofc,\正在转义“,但我仍然想删除它..
标签: c# sql regex postgresql replace