1

I've got a huge JSON String that I provide to this function. I need to replace some entries as they contain spaces and make my life difficult at a later time (JSON Parser).
Now the first line works flawlessly. The last line, however doesn't. To ensure I have something to be found I tried the index (line 2 and 3). So line 2 provides me a (correct) result, so the String is actually found. Line 3 doesn't (and that's fine).

        JSON = JSON.Replace(@"cap type", "CapType");
        int index = JSON.IndexOf("planning cycle [M\\u0026S]");
        int index2 = JSON.IndexOf("planning cycle [M&S]");
        JSON = JSON.Replace(@"planning cycle [M\\u0026S]", "PlanningCycleMS");

My thoughts are going towards escape sequence, like you have "\" instead of "\ for the "[" and "]".

So my question is, what can I do to make line 4 work, or get the result of that string be replaced for all occurrences in my data file?
thx

Janbro
  • 69
  • 6

1 Answers1

2

In line 2, you are using normal string "...", the \\ is translated to a single character \

In line 4, you are using verbatim string @"...", so each \ is a different character, so you have two chars \\

It is not the same string in these two lines.

lmcarreiro
  • 5,312
  • 7
  • 36
  • 63
  • 1
    @Janbro: To add a little more to this answer you might want to look at https://stackoverflow.com/a/3312075 which explains the difference between verbatim and normal string literals. Your usage suggests you aren't clear on what they do... – Chris May 22 '17 at 12:19
  • @Chris, thx for the link, but I simply didn't see the @ still in there. – Janbro May 22 '17 at 13:33
  • @Janbro: Ah, the reason I thought you might not be familar with it was the first use where the @ makes absolutely no difference so I assumed they were a copy and paste job without understanding. Glad you are sorted anyway. ;-) – Chris May 22 '17 at 14:39