locked
Split sting and pass as list of string. RRS feed

  • Question

  • User-148788041 posted

    Hi

    I have textbox that has string like this

    test.Text="C:\Card\abc.pdf;C:\New\Aline.txt;D:\Test\Windows.txt"
    string[] str = Test.Text.Split(";")
    i need to get abc.pdf,Aline.txt and Windows.tt to list of string 
    
    foreach(var st in str)
    {
    
    }
    list<string> lst=new list<string>();
    
    

    Wednesday, July 25, 2018 5:31 PM

Answers

  • User-1171043462 posted
    test.Text = @"C:\Card\abc.pdf;C:\New\Aline.txt;D:\Test\Windows.txt";
    List<string> str = test.Text.Split(';').ToList();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2018 7:21 PM
  • User-1715638843 posted

    first split with ;

    u will get 

    C:\Card\abc.pdf,
    C:\New\Aline.txt
    D:\Test\Windows.txt
    again split with '\' and take [2] position data to get what u required
    Mark as answer if correct
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 4:49 AM

All replies

  • User753101303 posted

    Hi,

    You could use https://msdn.microsoft.com/en-us/library/fkbw11z0(v=vs.110).aspx to create a list from anything that could be enumerated :

    var str = "a;b;c";
    var lst = new List<string>(str.Split(';')); // Create a list from an array

    Wednesday, July 25, 2018 5:44 PM
  • User-1171043462 posted
    test.Text = @"C:\Card\abc.pdf;C:\New\Aline.txt;D:\Test\Windows.txt";
    List<string> str = test.Text.Split(';').ToList();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 25, 2018 7:21 PM
  • User-148788041 posted

    After that how to get these values

    1. abc.pdf
    2. Aline.txt
    3. Windows.txt
    Thursday, July 26, 2018 2:20 AM
  • User-1715638843 posted

    first split with ;

    u will get 

    C:\Card\abc.pdf,
    C:\New\Aline.txt
    D:\Test\Windows.txt
    again split with '\' and take [2] position data to get what u required
    Mark as answer if correct
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 4:49 AM
  • User-1171043462 posted
    test.Text = @"C:\Card\abc.pdf;C:\New\Aline.txt;D:\Test\Windows.txt";
            List<string> str = test.Text.Split(';').ToList();
            //Using Loop
            foreach (string s in str)
            {
                string value = s;
            }
    
            //Also you can get by Index
            string value1 = str[0];
            string value2 = str[1];
            string value3 = str[2];

    Thursday, July 26, 2018 6:57 AM
  • User36583972 posted


    Hi Guhananth,

    After that how to get these values

    1. abc.pdf
    2. Aline.txt
    3. Windows.txt

    You can try the following code.

        List<string> finename = new List<string>();
                string tet = @"C:\Card\abc.pdf;C:\New\Aline.txt;D:\Test\Windows.txt";
                string[] str = tet.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var st in str)
                {
                    string result = Regex.Match(st, @"[^\\]+$").Value;
                    finename.Add(result);
                }
    

    Best Regards,

    Yong Lu

    Thursday, July 26, 2018 7:23 AM