Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have a string array, from which I want part of strings to bind dropdownlist


Ex:
C#
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

C#
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
 
Share this answer
 
The following LINQ expression

C#
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
 
Share this answer
 
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.
C#
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>");
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900