Parse a string with name-value pairs

How can I parse the following line of a name-value pair in C #:

string studentDetail = "StudentId=J1123,FirstName=Jack,LastName=Welch,StudentId=k3342,FirstName=Steve,LastName=Smith"

The goal of parsing this array is to insert values ​​into DB using Linq in SQL:

    [HttpPost()]
    public ActionResult SaveStudent(string studentDetail)
    {
        DataContext db = new DataContext();         

                Student student = new Student();
                {
                    student.StudentID = //StudentID
                    student.FirstName = //FirstName
                    student.LastName = //LastName

                };

                db.Student.InsertOnSubmit(student);
                db.SubmitChanges();

            return View();
    }

What is the best way to approach this?

+12
source share
4 answers

You can divide by a comma and then by an equal sign. I put the data in a dictionary for easy access.

string input = "StudentId=J1123,FirstName=Jack,LastName=Welch";

Dictionary<string,string> keyValuePairs = input.Split(',')
  .Select(value => value.Split('='))
  .ToDictionary(pair => pair[0], pair => pair[1]);

string studentId = keyValuePairs["StudentId"];

Note that this does not check input at all to ensure that there are no commas in the values, keys without values, missing keys, etc.

+43
source

Since individual student entries are not limited in input, I would do the following:

public class Student
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
} 

and then:

private List<Student> DoSplit(string input)
{
var theReturn = new List<Student>();
input = input.Replace(",StudentId=", "|,StudentId=");

var students = input.Split('|');

foreach (var student in students)
{
    var attribs = student.Split(',');
    if (attribs.Count() == 3)
    {
        var s = new Student();
        s.Id = attribs[0].Substring(attribs[0].LastIndexOf('='));
        s.FirstName = attribs[1].Substring(attribs[1].LastIndexOf('='));
        s.LastName = attribs[2].Substring(attribs[2].LastIndexOf('='));

        theReturn.Add(s);
    }
}

return theReturn;
}

, , "=", "," "|", . .

+2
string sourceStr= "StudentId=J1123,FirstName=Jack,LastName=Welch,StudentId=k3342,FirstName=Steve,LastName=Smith";

Dictionary<string,string> names=new Dictionary<string,string>();
string[] arr = sourceStr.Split(',');         
foreach (var ar1 in arr)
{
    string[] itemArr= ar1.Split('=');
    if (itemArr.Length > 1)
    {
        names.Add(itemArr[0], itemArr[1]);
    }
}
//check names now
+1
source

Eric Petroelier responded very well to fooobar.com/questions/424004 / ...

Try System.Web.HttpUtility.ParseQueryString , passing by just after the question mark. You will need to use the System.Web assembly, but this does not require a web context.

(I'm not sure why these two questions are not related)

I was able to parse the form string a = 1 & b = 2 & c = 3 using the following code

NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(submission);
0
source