0

I splitted csv-file by "," but some numbers were written with using of this symbol so some numbers were splitted on some parts. The beginning and the end of the every splitted number are signed by """ so i tried to paste parts of the number focusing on quotation marks...

string[] inputData = File.ReadAllLines("file.csv"); 
        string[,] allData = new string[inputData.GetLength(0) - 1, 8];
        for (int i = 1; i < inputData.GetLength(0); i++)
        {
            string[] arrayHelpStupied = inputData[i].Split(",");
            string[] arrayHelpSmart = new string[8];
            int m = 0;
            int flagMistake = 0;
            string compoundingOfSplittedNumber = "";
            Console.WriteLine("\u0022");
            for (int j = 0; j < arrayHelpStupied.Length; j++)
            {
                string prov = arrayHelpStupied[j];
                if ((prov[0].Equals("\"")) || (flagMistake == 1))
                {
                    flagMistake = 1;
                    compoundingOfSplittedNumber += arrayHelpStupied[j];
                    if (arrayHelpStupied[j][^1].Equals("\u0022"))
                    {
                        flagMistake = 0;
                        arrayHelpSmart[m++] = compoundingOfSplittedNumber;
                        continue;
                    }
                    continue;
                }
                arrayHelpSmart[m++] = arrayHelpStupied[j];
            }

but numbers that start with quotation mark is ignored :( please could you explain me the reason of such behaviour and how can i cope this difficulty please

  • Just use this library: https://joshclose.github.io/CsvHelper/ – bashis Nov 07 '22 at 00:43
  • unfortunatelly only build-in libraries are allowed – truly trying student Nov 07 '22 at 00:46
  • Please re-read the [mre] guidance on posting code and [edit] question to add/remove information. In particular provide expected input/output (as text), probably 1-2 lines. While reviewing your code make sure to simply expressions and limit code to only one that directly related to the problem. I.e. title claims that some particular *char* is "skipped" when "written in unicode" - make sure to show at least on `char` and clarify what you mean "written in unicode".... – Alexei Levenkov Nov 07 '22 at 01:04
  • ... It is possible that as "written in unicode" you mean `\uxxxx` [Unicode escape sequences](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#string-escape-sequences) - but even then there are no `char` shown in the code using that notation... Ideally you show just a short example with clearly indicated types (no `var`) and clear line where problem happens. I.e. `char x = "ab\""[^1]; string s = "\u0022"; Console.WriteLine(c.Equals(s));` – Alexei Levenkov Nov 07 '22 at 01:07
  • In the future, you need to put more work and more thought into your questions. Show a couple of rows or CSV data, each with a handful of columns. Tell what works and what doesn't - and why you think/guess the problemis happening. Remember, we haven't been working on your problem for several hours like you have. Until you tell us something, we don't have a clue what your talking about. Try to purge your mind of what you know about your problem and re-read your first paragraph. – Flydog57 Nov 07 '22 at 01:17
  • What is the `c#-6.0` time-warp all about? – Stefan Wuebbe Nov 07 '22 at 03:39

1 Answers1

0

If I'm reading the question correctly, you have a CSV file you're splitting on , and some of the "values" you're looking for also have a , in them and you don't want it to split on the , in the value...

Sorry, but it's not going to work. String.Split does not have any overrides, or regex matching. Your best bet is to sanitize your data first by removing the , from the values, or ensuring the , separating your values matches differently than the , in the values.

For example...

12,345 , 23,1566 , 111 , 1

you can split the above on " , " instead of just "," and since the pattern of " , " is consistent, then it will work

Alternately, you could open your csv in Excel and save it as a Tab delimited text file and then split on the tab "/t" character

Kevon
  • 984
  • 8
  • 28
  • I think what OP meant is that his CSV contains "escaped" values which is completely valid CSV input: https://stackoverflow.com/a/4617967/2560087 – bashis Nov 07 '22 at 00:51