-4

i have a semicolon separates string, that contains values of every type. string and date values are in quotations.

Now I have an evil string, where an inner string contains s semicolon, that I need to remove (replace by nothing).

eg:

"Value1";0;"Value2";4711;"Evil; Value";"2015-09-03"

in C#:

string value = "\"Value1\";0;\"Value2\";4711;\"Evil; Value\";\"2015-09-03\""

So how to replace all semicolons, that are in quotations?

Sayse
  • 42,633
  • 14
  • 77
  • 146
Dosihris
  • 85
  • 1
  • 8
  • yeah this sounds like a duplicate to me –  Sep 04 '15 at 08:27
  • In your example, I can't see a semi colon thats inside of quotes... but then judging by the fact you've already answered your own question, are you still looking for an answer? – Sayse Sep 04 '15 at 08:28
  • If you actually paid attention to the solutions on your other question, you'd realize that FileHelpers work with strings and streams as well. – Luaan Sep 04 '15 at 08:29
  • @Dosihris FYI TextFieldParser is not limited to work with FILES. You can also use it with STRINGS. – Adriano Repetti Sep 04 '15 at 08:29
  • 1
    And the next time, when your question is closed as a duplicate (or for any other reason) and you disagree, make it plain in the comments. Creating the exactly same question again *isn't going to help* - it'll just get closed again (as you can see). – Luaan Sep 04 '15 at 08:32
  • Adriano, can you post me an example using it with a string? I only see a construcotor with a string that is the path to a FILE. – Dosihris Sep 04 '15 at 08:32
  • Luaan, right now i dont even see that its a duplicate. – Dosihris Sep 04 '15 at 08:35
  • I suggest using LINQtoCSV. Have a look at [How to parse CSV that is passed as a parameter to a method](http://stackoverflow.com/questions/18959797/how-to-parse-csv-that-is-passed-as-a-parameter-to-a-method). And you can always `Regex.Split` by a [semi-colon outside of quotes](http://stackoverflow.com/questions/632475/regex-to-pick-commas-outside-of-quotes). – Wiktor Stribiżew Sep 04 '15 at 08:38
  • 1
    @Dosihris There is a constructor that accepts a Stream. You can build a stream around a string with StringReader("...") – Adriano Repetti Sep 04 '15 at 09:23

1 Answers1

-4

Before somebody is marking it as duplicate again, i will post the right answer now!

        StringBuilder builder = new StringBuilder(line);

        foreach (Match m in Regex.Matches(builder.ToString(), "\".*?\""))
        {
            if (m.Value.Contains(";"))      // If it contains a semicolon
            {
                string temp = m.Value.Replace(";", "");
                builder.Replace(m.Value, temp);
            }
        }

        var parts = builder.ToString().Split(new char[] { ';' });
Dosihris
  • 85
  • 1
  • 8