15

I am converting VB.NET code to c# and I am stopped when I reached the following code snippet. I need someone's help to convert Chr(34). Please help me to convert it to c#.

VB.NET Code

Dim inputString As String
inputString = "Some text ..."
inputString = Replace(inputString, Chr(34), "")**

My c# conversion is

string inputString = "Some text ...";
inputString = inputString.Replace(NEED HELP HERE, "");**
Trevor
  • 7,777
  • 6
  • 31
  • 50
Neetisa
  • 195
  • 1
  • 1
  • 8
  • You have choices you can use an escape so put a backslash or you can (char)34 – BugFinder Sep 19 '17 at 11:14
  • 7
    34 is `"` so replace with `"\""` – Alex K. Sep 19 '17 at 11:14
  • yep - its as simple as that - See Willem who has bother to write out more detail – BugFinder Sep 19 '17 at 11:18
  • 4
    Possible duplicate of [How to add double quotes to a string that is inside a variable?](https://stackoverflow.com/questions/3905946/how-to-add-double-quotes-to-a-string-that-is-inside-a-variable) – BugFinder Sep 19 '17 at 11:19
  • 4
    For what it's worth, using `Chr(34)` in VB ought to be discouraged, since it's lengthy and not very readable. The way to escape a double-quote character in a string literal in VB is to put two in a row, so the equivalent would be `Replace(inputString, """", "")`. – Steven Doggart Sep 19 '17 at 12:09
  • And the equivalent for that would be `Replace(inputString, @"""", "")` – Tom Blodget Sep 19 '17 at 14:16
  • 2
    Wow, I can not believe this question received this much attention when honestly it is something you should already know before asking this type of question... I mean there is only over 430k hits on Google?.? – Trevor Sep 19 '17 at 15:46
  • 1
    @Codexer Wonders of the World #8 : StackOverflow :D – boop_the_snoot Sep 19 '17 at 16:21
  • @o_O no doubt about that... :) – Trevor Sep 19 '17 at 16:33

2 Answers2

35

You can cast an integer to a char, so an "automatic" translation would be:

inputString = inputString.Replace(, ((char) 34).ToString(), "")

That being said, the characters that maps to 34 (according to ASCII) is ", so you can simply construct a string with the double quote char:

inputString = inputString.Replace(, "\"", "")

This will also improve readability, since it is now clear what you are doing in that part: you remove double quotation characters.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 4
    Sure that works because Chr(**34**) is always returns [U+0022](http://www.unicode.org/charts/nameslist/index.html). But in general, casting works for the range 0 to 255 yet is correct for the whole range 128 to 255 only if the user's (thread's) ANSI code page (ACP) is 28591 (ISO 8859-1). Too many gotchas to recommend. Of course, that assumes that using `Chr` isn't a latent bug to begin with. – Tom Blodget Sep 19 '17 at 14:25
5

Converted code below:

string inputString = null;
inputString = "Some text ...";
inputString = Strings.Replace(inputString, Strings.Chr(34), "");

I would recommend to use any converter if you are not dealing any confidential details. This will save sometime.

In fact, I used this tool to convert your code.

Karthick Raju
  • 757
  • 8
  • 29