0

I'm running into a problem where I'm reading a CSV file and I don't see any '\' in the file.

When I split the string by ',' and store it into a string variable, this is what the string looks like. "\"3040063816\""

The whole entire string is this before spliting

"\"3040063816\",\"123456789\",\"0.00\",\"0.00\",\"-95.99\",\"10/28/19\",\"09:04:11\",\"1\"\r"

How do you remove '\' from the string?

Because when I try to convert/parse the number into an INT, it gives me an error. I've tried to replace(@"\", string.empty) and it doesn't work.

Jawad
  • 11,028
  • 3
  • 24
  • 37
user3813162
  • 47
  • 2
  • 10
  • 8
    It doesn't actually exist. \ is displayed in the watch windows to denote an escaped special character, like `"`. It doesn't actually exist in the text itself. – Ron Beyer Dec 30 '19 at 21:42
  • try replace using str.Replace("\"","") – Matt.G Dec 30 '19 at 21:48
  • ... so what you want to remove are not backslashes, but quotes. Then you can parse. – Bent Tranberg Dec 30 '19 at 21:48
  • To @RonBeyer's point, if you're inspecting a `string` variable in the Visual Studio debugger - whether by hovering over a variable, in a `Watch` window, or `QuickWatch` - click the magnifying glass icon to open the [`Text Visualizer` window](https://learn.microsoft.com/visualstudio/debugger/string-visualizer-dialog-box), which will display the text with escaped characters rendered. You will see the expression `"\"3040063816\""` becomes `"3040063816"`. – Lance U. Matthews Dec 30 '19 at 21:54
  • If the escaping are "really" in the string, and not an artifact of VIsual Studio, then it looks as though the CSV might have been serialized to JSON. You need to deserialize if from JSON before you can parse it as CSV. See: https://dotnetfiddle.net/LcvBx4. Somebody put this on hold before I could answer. – dbc Dec 30 '19 at 22:12
  • But if the escaping isn't really in the string see [Parsing CSV files in C#, with header](https://stackoverflow.com/q/2081418/3744182) and [Reading CSV files using C#](https://stackoverflow.com/q/3507498/3744182). – dbc Dec 30 '19 at 22:13

1 Answers1

3

Try the following. This should get you going.


    string input = "\"3040063816\",\"123456789\",\"0.00\",\"0.00\",\"-95.99\",\"10/28/19\",\"09:04:11\",\"1\"\r";
    List<string> stringArr = input.Split(',').Select(x => x.Replace(@"""", "")).ToList();

    Console.WriteLine(Convert.ToInt64(stringArr[0])); // This is a large number. Has to be int64.
    Console.WriteLine(int.Parse(stringArr[1]));
    Console.WriteLine(float.Parse(stringArr[2]));
    Console.WriteLine(float.Parse(stringArr[3]));
    Console.WriteLine(float.Parse(stringArr[4]));
    Console.WriteLine(DateTime.Parse(stringArr[5]));
    Console.WriteLine(DateTime.Parse(stringArr[6]));
    Console.WriteLine(int.Parse(stringArr[7]));

Output

3040063816
123456789
0
0
-95.99
10/28/2019 12:00:00 AM
12/30/2019 9:04:11 AM
1

Each of the strings inside your string has double quotes in it. To remove that and make it a number representation, remove the quotes from each of the string after splitting it.

Review this post for conversion of large numbers and which format they can be converted to.

Jawad
  • 11,028
  • 3
  • 24
  • 37