C#, a String's Split() method

前端 未结 5 1199
终归单人心
终归单人心 2020-12-28 16:30

C#, a String\'s Split() method, how can I put the resulting string[] into an ArrayList or Stack?

相关标签:
5条回答
  • 2020-12-28 17:09

    protected void Button1_Click(object sender, EventArgs e) {

                TextBox1.Text = "Nitin Luhar";
                Array name=TextBox1.Text.Split(' ');
                foreach (string item in name)
                {
                    for (int i = 0; i < item.Length; i++)
                    {
                        if (i == 0)
                        {
                            Label1.Text = name.GetValue(0).ToString();
                        }
                        if (i == 1)
                        {
                            Label2.Text = name.GetValue(1).ToString();
                        }
                        if (i == 2)
                        {
                            Label3.Text = name.GetValue(2).ToString();
                        }
    
    
                    }
                }
    
    
    } 
    
    0 讨论(0)
  • 2020-12-28 17:14

    You can initialize a List<T> with an array (or any other object that implements IEnumerable). You should prefer the strongly typed List<T> over ArrayList.

    var myList = new List<string>(myString.Split(','));
    
    0 讨论(0)
  • 2020-12-28 17:24
    string[] strs = "Hello,You".Split(',');
    ArrayList al = new ArrayList();
    al.AddRange(strs);
    
    0 讨论(0)
  • 2020-12-28 17:31

    If you want a re-usable method, you could write an extension method.

    public static ArrayList ToArrayList(this IEnumerable enumerable) {  
      var list = new ArrayList;
      for ( var cur in enumerable ) {
        list.Add(cur);
      }
      return list;
    }
    
    public static Stack ToStack(this IEnumerable enumerable) {
      return new Stack(enumerable.ToArrayList());
    }
    
    var list = "hello wolrld".Split(' ').ToArrayList();
    
    0 讨论(0)
  • 2020-12-28 17:31

    Or if you insist on an ArrayList or Stack

    string myString = "1,2,3,4,5";
    ArrayList al = new ArrayList(myString.Split(','));
    Stack st = new Stack(myString.Split(','));
    
    0 讨论(0)
提交回复
热议问题