. NET data type conversion method,. net data type

Source: Internet
Author: User

. NET data type conversion method,. net data type

Using Newtonsoft. Json;
Using Newtonsoft. Json. Converters;
Using System. Web. Script. Serialization;

 

/// <Summary>
/// Convert string to dictionary type
/// </Summary>
/// <Param name = "value"> </param>
/// <Returns> </returns>
Public Dictionary <string, int> Str2Dic (string value)
{
Dictionary <string, int> IDs = new Dictionary <string, int> ();
String [] str1 = value. Split (';');
For (int I = 0; I <str1.Length; I ++)
{
String [] str2 = str1 [I]. Split (',');
If (! IDs. ContainsKey (str2 [0]) & str2 [0]! = "")
{
IDs. Add (str2 [0], int. Parse (str2 [1]);
}
}
Return IDs;
}

Public Dictionary <string, string> StrToDic (string value)
{
Dictionary <string, string> list = new Dictionary <string, string> ();
String [] str1 = value. Split ('#');
For (int I = 0; I <str1.Length; I ++)
{
String [] str2 = str1 [I]. Split (';');
If (! List. ContainsKey (str2 [0]) & str2 [0]! = "")
{
List. Add (str2 [0], str2 [1]);
}
}
Return list;
}

// Convert dictionary to string
Public string DicToStr (Dictionary <string, string> list)
{
String result = "";
Foreach (KeyValuePair <string, string> l in list)
{
Result + = l. Key + ";" + l. Value + "#";
}
Return result;
}

// List to string

Public string ListToStr (List <string> list)
{
String result = "";
Foreach (string s in list)
{
Result + = s + ",";
}
Return result;
}

 


Public List <string> StrToList (string str)
{
List <string> list = new List <string> ();
String [] s = str. Split (',');
For (int I = 0; I <s. Length; I ++)
{
If (s [I]! = ""&&! List. Contains (s [I])
{
List. Add (s [I]);
}
}
Return list;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.