Question

I have a string that looks like this

2,"E2002084700801601390870F"
3,"E2002084700801601390870F"
1,"E2002084700801601390870F"
4,"E2002084700801601390870F"
3,"E2002084700801601390870F"

This is one whole string, you can imagine it being on one row.

And I want to split this in the way they stand right now like this

2,"E2002084700801601390870F"

I cannot change the way it is formatted. So my best bet is to split at every second quotation mark. But I haven't found any good ways to do this. I've tried this https://stackoverflow.com/a/17892392/2914876 But I only get an error about invalid arguements.

Another issue is that this project is running .NET 2.0 so most LINQ functions aren't available.

Thank you.

Était-ce utile?

La solution

Try this

var regEx = new Regex(@"\d+\,"".*?""");
var lines = regex.Matches(txt).OfType<Match>().Select(m => m.Value).ToArray();

Use foreach instead of LINQ Select on .Net 2

Regex regEx = new Regex(@"\d+\,"".*?""");
foreach(Match m in regex.Matches(txt))
{
    var curLine = m.Value;
}

Autres conseils

I see three possibilities, none of them are particularly exciting.

  • As @dvnrrs suggests, if there's no comma where you have line-breaks, you should be in great shape. Replace ," with something novel. Replace the remaining "s with what you need. Replace the "something novel" with ," to restore them. This is probably the most solid--it solves the problem without much room for bugs.

  • Iterate through the string looking for the index of the next " from the previous index, and maintain a state machine to decide whether to manipulate it or not.

  • Split the string on "s and rejoin them in whatever way works the best for your application.

I realize regular expressions will handle this but here's a pure 2.0 way to handle as well. It's much more readable and maintainable in my humble opinion.

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            const string data = @"2,""E2002084700801601390870F""3,""E2002084700801601390870F""1,""E2002084700801601390870F""4,""E2002084700801601390870F""3,""E2002084700801601390870F""";

            var parsedData = ParseData(data);

            foreach (var parsedDatum in parsedData)
            {
                Console.WriteLine(parsedDatum);
            }

            Console.ReadLine();
        }

        private static IEnumerable<string> ParseData(string data)
        {
            var results = new List<string>();
            var split = data.Split(new [] {'"'}, StringSplitOptions.RemoveEmptyEntries);

            if (split.Length % 2 != 0)
            {
                throw new Exception("Data Formatting Error");
            }

            for (var index = 0; index < split.Length / 2; index += 2)
            {
                results.Add(string.Format(@"""{0}""{1}""", split[index], split[index + 1]));
            }

            return results;
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top