Click here to Skip to main content
14,303,913 members
Rate this:
Please Sign up or sign in to vote.
Hi
I have a string array, from which I want part of strings to bind dropdownlist


Ex:
string[] strArray = new string[10];
          
strArray[0] = "C:\MyFolder\Software";
strArray[1] = "C:\MyFolder\Admin";
strArray[2] = "C:\MyFolder\Accounts";
strArray[3] = "C:\MyFolder\HR";
strArray[4] = "C:\MyFolder\Maintainece";


From this string array i need "Software,Admin,Accounts,HR,Maintainece"
these strings to bind in dropdownlist without using foreach or for loop.

I need this using linq technique.. Pls help me
Posted
Updated 14-Mar-12 18:29pm
v2
Comments
ProEnggSoft 15-Mar-12 0:30am
   
Edit: pre tag for C# code added - PES
Rate this:
Please Sign up or sign in to vote.

Solution 1

for getting splited string you can use Split method of string datatype.
for more information on Split method can see this.
Split Method[^]

you can go like this.
string[] strArray = new string[5];
strArray[0] = @"C:\MyFolder\Software";
strArray[1] = @"C:\MyFolder\Admin";
strArray[2] = @"C:\MyFolder\Accounts";
strArray[3] = @"C:\MyFolder\HR";
strArray[4] = @"C:\MyFolder\Maintainece";

foreach (string str in strArray)
{
    string[] words =str.Split('\\');
    Response.Write(words[2] + ",<br>");
}
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

The following LINQ expression

IEnumerable<string> ItemList = strArray
    .Where (a => a != null && a.Contains('\\'))
    .Select (a => a.Substring(a.LastIndexOf('\\')+1));


gives a collection strings, which can be bound to DropDownList
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

string[] strArray = new string[5];
strArray[0] = @"C:\MyFolder\Software";
strArray[1] = @"C:\MyFolder\Admin";
strArray[2] = @"C:\MyFolder\Accounts";
strArray[3] = @"C:\MyFolder\HR";
strArray[4] = @"C:\MyFolder\Maintainece";

foreach (string node in strArray)
{
    string[] strpart =node.Split('\\');
    DropdownList1.Items.Add(strpart[strpart.Length-1]);
}


Get last string By this code
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100