none
How to add array elements to a list adding an equals sign and number to each line RRS feed

  • Question

  • I have a string of numbers like so:

      string[] strings  = { "10", "5", "3", "8", "72", "15", "92", "6", "12" } ;
    

    I found the count like so:

    int count= Convert.ToInt32( strings.Count( ).ToString( ) );

    The count is '9'.

    I know that the index of an array starts at 0, how can I add all these to a list so the list looks like this:

    1="10"

    2="5"

    3="3"  and so on?

    List<string> myList = new List<string>( ) ;
    
    foreach(string line in strings)
    {
      myList.Add( line + "=" + the number) ; e.g. "1=10"
    }
    




    CuriousCoder

    Monday, November 19, 2018 1:08 PM

Answers


  •             List<string> list = new List<string>();

                int i = 0;

               
                foreach (var item in strings)
                {
                    list.Add($"{i+1}= {strings[i]}");
                    i++;
                }

                foreach (var item in list)
                {
                    Console.WriteLine(item);
                }

    Try indexing from zero whenever possible as that will cause you less agony :)







    • Edited by ThisNewbie Monday, November 19, 2018 3:51 PM
    • Marked as answer by CuriousCoder15 Monday, November 19, 2018 4:08 PM
    Monday, November 19, 2018 3:27 PM

All replies

  • See Array Class first.

    Review your code. Why converting a number ToString() and then back to Int32?

    And the rest is a formatted output like e.g. E.g.

    namespace ConsoleCS
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
    
        class Program
        {
            static void Main(string[] args)
            {
                string[] stringList = { "10", "5", "3", "8", "72", "15", "92", "6", "12" };
                Console.WriteLine($"Array size: {stringList.Length}\n");
    
                string concatenatedStringList = null;
                foreach (string stringItem in stringList)
                {
                    concatenatedStringList += stringItem;
                }
    
                Console.WriteLine($"Concatenated: {concatenatedStringList}\n");
    
                Console.WriteLine("Indexed list:");
                List<string> formattedStrings = new List<string>();
                for (int index = 0; index < stringList.Length; index++)
                {
                    string formattedString = $"{index}: {stringList[index]}";
                    formattedStrings.Add(formattedString);
                    Console.WriteLine(formattedString);
                }
    
                formattedStrings.ToConsole<string>("\nFormatted list:");
    
                stringList
                    .Select((value, index) => new { value, index })
                    .ToConsole("\nFormatted list (lambda/LINQ):", s => $"{s.index}: {s.value}");
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
        }
    }

    with

    namespace ConsoleCS
    {
        using System;
        using System.Collections.Generic;
    
        public static class EnumerableHelper
        {
            public static void ToConsole<T>(this IEnumerable<T> enumerable)
            {
                foreach (T item in enumerable)
                {
                    Console.WriteLine(item);
                }
            }
    
            public static void ToConsole<T>(this IEnumerable<T> enumerable, string caption)
            {
                Console.WriteLine(caption);
                foreach (T item in enumerable)
                {
                    Console.WriteLine(item);
                }
    
                Console.WriteLine("");
            }
    
            public static void ToConsole<T>(this IEnumerable<T> enumerable, Func<T, string> toString)
            {
                foreach (T item in enumerable)
                {
                    Console.WriteLine(toString(item));
                }
            }
    
            public static void ToConsole<T>(this IEnumerable<T> enumerable, string caption, Func<T, string> toString)
            {
                Console.WriteLine(caption);
                foreach (T item in enumerable)
                {
                    Console.WriteLine(toString(item));
                }
    
                Console.WriteLine("");
            }
        }
    }

    p.s. it's important that you learn to craft concise and complete examples. Post them instead of fragments.



    • Edited by Stefan Hoffmann Monday, November 19, 2018 2:57 PM No solution without LINQ ;)
    Monday, November 19, 2018 1:22 PM
  • Hi,

    The numbers were originally part of a text file I read and split the string line to get to the numbers. I will read through your example and try to learn what you are doing.

    Thank you


    CuriousCoder

    Monday, November 19, 2018 3:20 PM

  •             List<string> list = new List<string>();

                int i = 0;

               
                foreach (var item in strings)
                {
                    list.Add($"{i+1}= {strings[i]}");
                    i++;
                }

                foreach (var item in list)
                {
                    Console.WriteLine(item);
                }

    Try indexing from zero whenever possible as that will cause you less agony :)







    • Edited by ThisNewbie Monday, November 19, 2018 3:51 PM
    • Marked as answer by CuriousCoder15 Monday, November 19, 2018 4:08 PM
    Monday, November 19, 2018 3:27 PM