C # List and string convert each other

Source: Internet
Author: User

List to string, separated by commas

list<string> list = new list<string> ();
List. ADD ("a");
List. ADD ("B");
List. ADD ("C");
MessageBox.Show (list.);
Loadmodel ();
string s = String. Join (",", list. ToArray ());
MessageBox.Show (s);

list<test> list = new list<test> ();
List. ADD (New Test ("1", "a"));
List. ADD (New Test ("2", "B"));
List. ADD (New Test ("", ""));
List. ADD (New Test ("3", "C"));
var a = from O in list select O.test1;
var B = from O in list select O.test2;
string S1 = string. Join (",", A.toarray ());
String s2 = string. Join (",", B.toarray ());
MessageBox.Show (S1 + "\ r \ n" + s2);

Results: 1,2,,3

A,b,,c

String Goto List

Here s the delimiter is not "," but ",", followed by a space

string s = "1, 2, 3";
list<string> list = new List<string> (S.split (new string[] {","}, Stringsplitoptions.removeemptyentries));
foreach (String T in list)
{
MessageBox.Show ("*" + t + "*");
}

Here s the delimiter is ","

string s = "All-in-all";
list<string> list = new list<string> (S.split (', '));
foreach (String T in list)
{
MessageBox.Show ("*" + t + "*");
}

Source: http://www.cnblogs.com/eric_ibm/p/list.html

List<int> Turn string:

List<int>: 1,2,3,4,5,6,7 converted to string: "1,2,3,4,5,6,7"

list<int> list= New List<int> () {1, 2, 3, 4, 5, 6, 7};string depaid = string. Join (",", list);

String turn list<int>:

string s = "1, 2, 3"; list<string> list = new List<string> (S.split (new string[] {","}, Stringsplitoptions.removeemptyentries));
list<string> list = new list<string> (S.split (', '));//or

List<string> Turn list<int>

var listOfStrings =(new[]{"4","5","6"}).ToList();
var listOfInts = listOfStrings.Select<string,int>(q =>Convert.ToInt32(q));

List<int> Turn list<string>

list<int> L1 = new list<int> (new int[] {}); list<string> L2 = L1. convertall<string> (x = x.tostring ());

C # string[] arrays and list<string>:

system.string[] str={"str", "String", "abc"}; list<system.string> lists=new list<system.string> (str);

Transfer from list<system.string> to system.string[]

List<system.string> lists=new list<system.string> (); Lists.add ("str"); Lists.add ("Hello"); System.string[] Str=lists.toarray ();

Source: http://www.cnblogs.com/chear/archive/2012/12/22/2828956.html

system.string[] strarr={"str", "String", "abc"};

func<string, string[]> func = (str) = =
{
if (str = = null)
str = string. Empty;
Return str. Split (', ');
};
ilist<system.int> ilists = Typeparse.tolistint (func (Strarr));

C # List and string convert each other

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.