c# - Unable to replace bools with ints -
i have sql strings such following, several trues
, falses
:
insert systemrules (col1, col2, col3, col4, col5) values (false,false,true,false,false)
i want replace false 0 , true 1. when try this:
sql = sql.replace(",true,", ",1,").replace(",true)", ",1)").replace(",false,", ",0,").replace(",false)", ",0)");
...it removes some of falses...not of them. example, end this:
insert systemrules (col1, col2, col3, col4, col5) values (0,false,1,0,false)
what expected this:
insert systemrules (col1, col2, col3, col4, col5) values (0,0,1,0,0)
so try regex instead (showing 1 piece below):
sql = regex.replace(sql, ",true)", ",1)");
that particular line blows error: parsing ",true)" - many )'s.
what efficient way replace trues , falses in sql statement 1s , 0s? have found string.replace() in c# replace globally, i'm baffled why it's missing some. there no spaces in there, have triple-checked. 1 proof of if run series of replaces above 2nd time, does replace stragglers. why not first time? c# replace
not global? thank you.
you error regex.replace
because )
metacharacter needs escaped. placing backspace (two backspaces regular c# string) address problem:
sql = regex.replace(sql, ",true\\)", ",1\\)");
you can simplify comma/parentheses handling \\b
anchor:
sql = regex.replace(sql, "\\btrue\\b", "1");
however, manipulating sql string dangerous idea (why?). should refactor code use parameterized sql instead (how?).
Comments
Post a Comment