Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,

I have an idea where I still not sure how to solve in practice.

I have a list of string, these strings are all formed by 6 digits. "012345". what I want is to add these strings in a list.

Now with this list made​​, I want to go through the whole list and go picking at each position, the first digit of the string. in the end I want to form an array with all digits caught.

example: I have a list {012345, 032165, 211458, 214563, 098796, 574623, 894235, 3135698}

to scroll through the list to form the new vector like this: [0, 0, 2, 2, 0, 5, 8, 3] which is the first value of each element of the list.

first part I want to solve this problem.
second part I will randomly select a position that any vector in the main list and go and do the following: I want to build or a new list with only the file name starts with digit values ​​drawn vector.

example: if the vector drawn the value "0", I want to build a new list with only those values​​: {012345, 032165, 098796}

any ideas? If I am not very clear on the idea, please tell me something
Posted
v2

To solve the first problem, use this code:
C#
List<string> yourList = new List<string>() { "012345", "032165", "211458", "214563", "098796", "574623", "894235", "3135698"};
List<string> firstDigits = yourList.Select(x => x.Substring(0, 1)).ToList();

About the second problem, I'm not sure, but try this, and tell me if this isn't what you want:
C#
Random r = new Random();
int max = yourList.Select(x => x.Length).Min();
int randomNum = r.Next(9);
List<string> newList = yourList.Where(x => x.StartsWith(randomNum.ToString());

Hope this helps.
 
Share this answer
 
The first part is easy:
C#
List<string> myList = new List<string>() { "012345", "032165", "211458", "214563", "098796", "574623", "894235", "3135698" };
char[] chars = new char[myList.Count];
int index = 0;
foreach (string s in myList)
    {
    chars[index++] = s.Length > 0 ? s[0] : '?';
    }
The second part is also pretty simple - all you have to do is add a list, and a simple condition to the foreach loop.

But since this sounds like homework, I'll leave that to you!
 
Share this answer
 
Comments
fasher_the_one 28-Aug-13 7:43am    
Thank you guys, all the solutions have served me but I used the solution of ProgramFOX to use lists

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