-2

Basically lets say I have used this:

string[] result = File.ReadAllText("C:\\file.txt");

Inside the .txt file is the following words/values (case sensitive):

Zack 2 5 5
Ben 5 3 4
Dom 2 4 6 

Currently I know whatever is read will be stored in the string array and it includes the name and numbers.

How do I split them so that the names are in one array and the numbers are converted into int array? Basically separating the numbers and names.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Andrew Tsang
  • 11
  • 1
  • 2
  • 5
    You already seem to know the terms "array", "split" and "convert". Can you please show what you have tried? You can start by adapting code from questions like [Easy string parsing](http://stackoverflow.com/questions/6792828/easy-string-parsing), found through "C# split string parse". – CodeCaster Nov 26 '15 at 19:42
  • Well I was able to read the file into the result string but that is as far as go without using stackoverflow. I don't have the code with me as of now (on different computer) so from what I done, I simply splitted the result string and it is in another string array called array0, but the problem is how do I split the numbers from the name? They are together still. In string form – Andrew Tsang Nov 26 '15 at 19:50
  • Just like the first time, after splitting the text from the file into lines, you can split each line into separate elements. There's also methods in the `File` class that let you read a file as a string array of lines. so you can skip the first step. Anyway it's not really useful to ask an "offline" question, given the fast nature of this site. When you sit down behind your development machine again, try to fiddle some more with the code and update your question with what you have. – CodeCaster Nov 26 '15 at 19:53
  • Ok i'll keep that in mind. Thanks didn't know you could split a second time. – Andrew Tsang Nov 26 '15 at 20:00

2 Answers2

0

Loop over each item, split by space, use the first value as a key, then use the rest of the values to create array of ints.

string[] result = { "Zack 2 5 5", "Ben 5 3 4", "Dom 2 4 6" };
var lookup = result.ToDictionary(
    key => key.Split(' ').First(), // first is name, ignore rest
    ints => ints.Split(' ')
        .Skip(1) // skip the name, ints are the rest
        .Select(i => int.Parse(i))
        .ToArray());
foreach (var kvp in lookup)
    Console.WriteLine(kvp.Key + " - " + string.Join(",", kvp.Value));

Output:

Zack - 2,5,5
Ben - 5,3,4
Dom - 2,4,6

I used Dictionary as the generated list. That's assuming that every item in the result array has a unique name value. If there are duplicates, you'll need to modify it to either append the new int values or use something else than a dictionary to keep your parsed values.

You can see this SO thread about the case-sensitivity of dictionaries (they arn't).

Community
  • 1
  • 1
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
0

For chars array:

string charsArray = new String(result.ToCharArray().Where(c => Char.IsLetter(c)).ToArray());

For numbers array;

string NumbersArray = new String(result.ToCharArray().Where(c => Char.IsDigit(c)).ToArray());

Decide just when and how to use them. good luck

  • one way of use is per row.
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43