Question

Hi I have a basic mistake because I do not look at is how to fix the code:

string code = 'dsad {"scans": dssadasd';
code = code.Replace('{"scans":','test');

the problem is that it fails due to strange characters in the first replacement

anyone can help me?

pd :

sorry here have the original line and I forget show, as I have to do the same with this line :

code = code.Replace('"(.*?)": {"detected": (.*?), "version": (.*?), "result": (.*?), "update": (.*?)}','test');
Was it helpful?

Solution

  1. in your first string, you should:

    • use \ to escape the double quotes inside the string;
    • or prepend the string with @ and escape with ""

       string code = "dsad {\"scans\": dssadasd";
       string code = @"dsad {""scans"": dssadasd";
      
  2. the ' character is used to delimit chars, not strings. You should use " instead.

    code = code.Replace("{\"scans\":","test");
    

OTHER TIPS

You have to scape the strings

string code = "dsad {\"scans\": dssadasd";
code = code.Replace("{\"scans\":","test");

That's not compilable C#. Strings are always double-quoted in C#:

string code = "dsad {\"scans\": dssadasd";
code = code.Replace("{\"scans\":", "test");

C# isn't JavaScript :)

That code is not a valid C#. You need to use double-quotes to represent a string and escape the double-quotes inside of the string:

string code = "dsad {\"scans\": dssadasd";
code = code.Replace("{\"scans\":","test");

Try this instead:

    string code = "dsad {\"scans\": dssadasd";
    code = code.Replace("{\"scans\":", "test");

Cheers

You need to escape the double quotes.

string code = "dsad {\"scans\": dssadasd";
code = code.Replace("{\"scans":',"test");

You need to escape the double quotes.

string code = "dsad {\"scans\": dssadasd";
code = code.Replace("{\"scans\":", "test");

You also need to use double quotes rather than single quotes for strings- single quotes are only for char values in C#.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top