5

Is there a C# method like Python's repr() to get the true representation of the object? Suppose we have:

string identifier = "22\n44";
Console.WriteLine(identifier);

This would return

22
44

Is there a way to get

"22\n44"

In Python this is easy. We can just do repr("22\n44").

I thought of this question because I was trying to convert "2244" to '2244' using

var identifier = "2244";
identifier = identifier.Replace("\"", "'");
Console.WriteLine(identifier);

The output is just 2244, because double quotes are for our purpose. But in my case, I did this to get what I wanted:

identifier = string.Format("'{0}'", identifier);

because initially the database was receiving the query as IN ("2244") instead of IN ('2244') and was throwing Invalid Number error.

Animesh
  • 4,926
  • 14
  • 68
  • 110
  • 4
    Don't do DB input like that, you're leaving the door open for [SQL injection](http://xkcd.com/327/). – Tim S. Oct 24 '13 at 16:19
  • 2
    And using `repr()` to 'escape' values for database insertion is just as wrong in Python as it is in C#. – Martijn Pieters Oct 24 '13 at 16:21
  • @GamesBrainiac: I didn't downvote, but this does read like a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Perhaps the OP should ask about the `Invalid Number` error instead. – Martijn Pieters Oct 24 '13 at 16:23
  • @MartijnPieters you can do `"22\n44".repr()` if you provide extension method. – Zbigniew Oct 24 '13 at 16:24
  • @walkhard: are we still talking about Python here? – Martijn Pieters Oct 24 '13 at 16:25
  • @TimS. I am aware of SQL Injection. However, in this case the value sent to database is not a user input, but a value parsed from the UI itself. – Animesh Oct 24 '13 at 16:25
  • 1
    @MartijnPieters I think what he's looking for is how to create a raw string in C#. My knowledge of C# is abysmal, so I might be wrong. Also, he _did_ show expected output. – Games Brainiac Oct 24 '13 at 16:26
  • @Animesh Regardless, if it's logically a number then it should be typed as a number, not a string, and inserted into the query as a number, not as a string. This will, among other things, assure that your numbers are always numbers and you *cannot possibly ever* have improperly formatted numbers in your query. – Servy Oct 24 '13 at 16:26
  • @MartijnPieters I though we're talking about `C# method like Python's repr()` – Zbigniew Oct 24 '13 at 16:26
  • 1
    @GamesBrainiac: Sure, but that's still an XY problem. The *actual* problem that the OP is solving is how to generate a `IN (...)` SQL query from variable input. – Martijn Pieters Oct 24 '13 at 16:27
  • @MartijnPieters You're right, there's noting about SQL here. – Games Brainiac Oct 24 '13 at 16:28
  • @walkhard: The OP claimed, before editing, that *in Python*, you'd simply use `str.repr()`, while no such method exists. – Martijn Pieters Oct 24 '13 at 16:28
  • @walkhard Keep in mind that the escape sequences for your database are entirely different than the escape sequences for strings in C#. Translating a query into what the escaped version would be in C# doesn't mean it'll be appropriate input for a database, so your `repr` method wouldn't actually work in this case, even if one existed. – Servy Oct 24 '13 at 16:28
  • @Servy well I got lost, on one way OP wants to get `"22\n44"` on other he wants to use it in `IN` as `2244` (without `\n`), so I've provided an answer for the question. You're right that escape sequences in database can be different than those in C#, yet it doesn't mean that it's an inappropriate input for a database. – Zbigniew Oct 24 '13 at 16:37
  • @walkhard There is some overlap, so it will sometimes work, and then it will sometimes fail. That doesn't make it an acceptable solution to the problem, it's just fixing the current example while still leaving a ticking time bomb around. – Servy Oct 24 '13 at 16:39
  • @Servy Ahh those are the best bugs, aren't they? Though if we're talking about the core problem here, then it seems that `2244` is a integer value, so there is no reason to have fun with escape sequences here. Anyways this question generated a lot of comments and an upvoted wrong answer. Perhaps I should consider removing it. – Zbigniew Oct 24 '13 at 16:48

1 Answers1

0
        string identifier = "22\n44";
        Console.WriteLine("\"{0}\"", identifier.Replace("\n", @"\n"));
Reda
  • 2,289
  • 17
  • 19