-1

How do I parse a string in C# to extract key-value pairs without any delimiters between key and value?

I have a string that looks like this

string str = "\nFIRSTNAMEJOHN\nLASTNAMESMITH\nADDRESS1590 GRACE STREET\nBIRTHDATE04201969"

After splitting on \n I get a collection of strings that looks like

string[] properties = ["FIRSTNAMEJOHN","LASTNAMESMITH", etc.]

I want to loop through the split array of these strings and extract the key-value pair from each individual string, so that I can populate the properties of an object such as...

Person person = new Person() 
{ FIRSTNAME = JOHN,
  LASTNAME = SMITH,
  etc...
}

What is the cleanest way of doing this? Thanks!

zjc2653
  • 1
  • 2
  • What are you getting your data from that is causing the data to have no delimeter? Are you sure there is not a unprintable character in there as the delimeter? – Scott Chamberlain Sep 30 '19 at 22:49
  • You can do this in a fairly straightforward manner. First, I used your string input. I create a public enum called FieldNames. To get the string value, I created a class called EnumStringValue that allows you to create an attribute for each enum member. Finally, I create a ParseString method which takes a string input. It splits on the '\n' as before, but inside this method it calls on a ParseLine method that takes each of the items in the split array. I can send if needed, there isn't enough room here to paste the whole thing. – Su Llewellyn Sep 30 '19 at 23:26

1 Answers1

4

You could create a class that has those properties, then get the properties of the object through reflection (so we can use a loop), split the string on the \n character, then for each property, and for each setting, if the setting starts with the property name, set the property value based on the rest of the setting string.

For example:

class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string BirthDate { get; set; }

    public override string ToString()
    {
        return $"{FirstName} {LastName}, born on {BirthDate}, lives at: {Address}";
    }
}

public class Program
{
    static void Main(string[] args)
    {
        string str = "\nFIRSTNAMEJOHN\nLASTNAMESMITH\nADDRESS1590 GRACE STREET\nBIRTHDATE04201969";
        var user = new User();
        var properties = typeof(User).GetProperties();
        var settings = str.Split('\n');

        foreach (var property in properties)
        {
            foreach (var setting in settings)
            {
                if (setting.StartsWith(property.Name, StringComparison.OrdinalIgnoreCase))
                {
                    property.SetValue(user, setting.Substring(property.Name.Length));
                    break;
                }
            }
        }

        Console.WriteLine(user);

        GetKeyFromUser("\n\nDone! Press any key to exit...");
    }
}

Output

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43