Split string to key value pairs
split-string-to-key-value-pairs.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//this section create a string variable.
string plant = "1 Cabbage";
Label1.Text = "string of plant..................<br />";
Label1.Text += plant;
//split string and create an array of two elements.
string[] array = plant.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
//create a new dictionary object.
Dictionary<int, string> dictionary = new Dictionary<int, string>();
//create and populate a new keyvaluepair from splitted string.
KeyValuePair<int, string> pair = new KeyValuePair<int, string>(Convert.ToInt32(array[0]),array[1]);
//dictionary add new keyvaluepair
dictionary.Add(pair.Key, pair.Value);
Label1.Text += "<br /><br />dictionary keys and values..........";
foreach (KeyValuePair<int, string> p in dictionary)
{
Label1.Text += "<br />" + p.Key + " ------- " + p.Value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - split string to key value pairs</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - split string to key value pairs
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="split string to key value pairs"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to remove a character from a string at specified position
- How to remove non alphanumeric characters from a string
- How to create a string by repeating a character
- How to remove white spaces from strating of a string
- How to format a string as datetime yyyyMMdd
- How to convert a string to a bool
- How to format a string as currency
- How to create a csv from a string
- How to convert a string to a float array
- How to get bytes from a string