guys i need an error-free way of keeping name value pairs in a regular c# string. i am quite busy, i dont think i am able to come up with the best algorithm now. what i want is two methods one to add a name-value pair to the string, the other is to get the value of that name.

Thanks for your help.

ddanbe commented: Thanks for the interesting question. +13

Recommended Answers

All 12 Replies

all my name-value pairs will in one string like a query string in url. is that what it comes to? i couldnt see it right away but i am busy too :)
dont try to answer to a busy guy if you are busy your self too :)

Following from http://www.albahari.com/nutshell/ch24.aspx

Extracting “name = value” pairs (one per line):

string r = @"(?m)^\s*(?'name'\w+)\s*=\s*(?'value'.*)\s*(?=\r?$)";

string text =
  @"id = 3
    secure = true
    timeout = 30";

foreach (Match m in Regex.Matches (text, r))
  Console.WriteLine (m.Groups["name"] + " is " + m.Groups["value"]);

id is 3
secure is true
timeout is 30

commented: What can you do if you are not busy? +13

He guys, thanks! I'm learning a lot here. :)

DdoubleD you're very active I like your way, keep it up.

Following from http://www.albahari.com/nutshell/ch24.aspx

Extracting “name = value” pairs (one per line):

string r = @"(?m)^\s*(?'name'\w+)\s*=\s*(?'value'.*)\s*(?=\r?$)";

string text =
  @"id = 3
    secure = true
    timeout = 30";

foreach (Match m in Regex.Matches (text, r))
  Console.WriteLine (m.Groups["name"] + " is " + m.Groups["value"]);

id is 3
secure is true
timeout is 30

Unfortunately i can't add to your reputation more than once a day.

By the way this albahari guy and i know each other, i am going to translate one of his tutorials(threading) into Turkish, we already made the deal but he said he is going to renew the tutorial, so i am waiting for him to complete.

DdoubleD you're very active I like your way, keep it up.

i am also active man, where is my reputation? the one who asks the quality question is valuable as the one who answers properly right?

commented: Yes, they also deserve rep. points ;) +11

DdoubleD you're very active I like your way, keep it up.

Just having fun boss and thanks! I'll look for the raise come review time...

By the way this albahari guy and i know each other, i am going to translate one of his tutorials(threading) into Turkish, we already made the deal but he said he is going to renew the tutorial, so i am waiting for him to complete.

It truly is a small world!;)

class1.cs :

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication4
{
    class Class1
    {
        // the name-value pairs will be like name1=value1&name2=value2& and so on
        string nameValueString = string.Empty;
        public Class1(string nameValueString)
        {
            this.nameValueString = nameValueString;
        }
        public string GetValue(string name)
        {
            try
            {
                char[] splitter = new char[] { '&' };
                string[] pairs = nameValueString.Split(splitter);
                foreach (string pair in pairs)
                {
                    if (pair.IndexOf(name)==0)
                    {
                        try
                        {
                            return pair.Substring(pair.IndexOf("=") + 1);
                        }
                        catch { }
                    }
                }
            }
            catch { }
            return "not found";
        }
        public void AddNew(string name, string value)
        {
            // using stringBuilder class would have been better as string type is immutable
            // and wasted memory.
            nameValueString = nameValueString + name + "=" + value + "&";
        }
    }
}

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{
    public partial class Form1 : Form
    {
        private Class1 nameValueObj = new Class1(string.Empty);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            nameValueObj.AddNew(textBox1.Text, textBox3.Text);
            MessageBox.Show("Pair added");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(nameValueObj.GetValue(textBox2.Text));
        }
    }
}

Form1.Designer.cs :

namespace WindowsApplication4
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(90, 40);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(75, 20);
            this.textBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(43, 74);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(296, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Append new name-value pair";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(40, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(138, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Enter your name-value pairs";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(106, 144);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 3;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(40, 151);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(63, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Enter Name";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(212, 141);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(127, 23);
            this.button2.TabIndex = 5;
            this.button2.Text = "Find the value";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(40, 43);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(35, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "Name";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(182, 47);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(34, 13);
            this.label4.TabIndex = 7;
            this.label4.Text = "Value";
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(222, 40);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(90, 20);
            this.textBox3.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(428, 282);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox textBox3;
    }
}
commented: very good question & solution too. +16

Here is my contribution,

NameValueCollection query = System.Web.HttpUtility.ParseQueryString("A=10&B=20&C=30");
Console.WriteLine(query["A"] + " " + query["B"] + " " + query["C"]);
commented: chic +9
commented: Really very nice (Y) +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.