c# - semi complex string replace? -


i have text

'random text', 'a\nb\\c\'d\\', 'ok' 

i want become

'random text', 'a\nb\c''d\', 'ok' 

the issue escaping. instead of escaping \ escape ' ''. 3rd party program can't change needing change 1 escaping method another.

the issue \\'. if string replace become \'' rather \'. \n not newline actual text \n shouldn't modified. tried using regex couldn't think of way if ' replace '' else if \\ replace \. doing in 2 step creates problem.

how replace string properly?

if understand question correctly, issue lies in replacing \\ \, can cause another replacement if occurs right before '. 1 technique replace intermediary string first you're sure not occur anywhere else, replace after you're done.

var str = @"'random text', 'a\nb\\c\'d\\', 'ok'"; str.replace(@"\\", "non_occurring_temp")     .replace(@"\'", "''")     .replace("non_occurring_temp", @"\"); 

as pointed out @alexeilevenkov, can use regex.replace both modifications simultaneously.

regex.replace(str, @"(\\\\)|(\\')",     match => match.value == @"\\" ? @"\" : @"''"); 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -