0

So I wrote a fonction called Afficher(int [,] m) which prints out in a table like manner the contents of a multidimensional array. The problem is that I would like the fonction to return a string instead of being just a void so that I can combine it to another string later on. Is there a possible way of doing this, maybe by using StringBuilder? Thanks.

        static void Afficher(int[,] m)
        {

            string lignePleine = new string('-', m.GetLength(1) * 6 + 1);

            for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
            {

                Console.WriteLine(lignePleine);
                Console.Write("|");
                for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
                {

                    {
                        Console.Write($"{m[ligne, colonne],3}  |");
                    }
                }
                Console.WriteLine();
            }

            Console.WriteLine(lignePleine);
            Console.WriteLine();


        }


        public int[,] CreationMatrice()
        {
            Random generateur = new Random();
            List<int> NewValeur = new();
            for (int i = 1; i <= (Trajet.NB_LIGNES * Trajet.NB_COLONNES); i++)
            {
                listeValeurs.Add(i);
            }
            int nombreValeur = listeValeurs.Count;
            for (int i = 0; i < nombreValeur; i++)
            {
                int indexRandom = generateur.Next(listeValeurs.Count);
                NewValeur.Add(listeValeurs[indexRandom]);
                listeValeurs.RemoveAt(indexRandom);
            }

            int index = 0;
            for (int ligne = 0; ligne < Trajet.NB_LIGNES; ligne++)
            {
                for (int colonne = 0; colonne < Trajet.NB_COLONNES; colonne++)
                {
                    matrice[ligne, colonne] = NewValeur[index];
                    index++;
                }
            }
            return matrice;
        }

2 Answers2

0

You can, instead of printing the parts that compose the string, simply add them to an object and return it at the end:

static string Afficher(int[,] m)
{
    string lignePleine = new string('-', m.GetLength(1) * 6 + 1);
    var result = "";
    for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
    {         
        result += lignePleine + "\n" + "|";

        for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
        {
            result += $"{m[ligne, colonne],3}  |";
        }
        result += "\n";
    }
    result += lignePleine + '\n';
    return result;
}

https://onlinegdb.com/G9ErwOcE6

Above simpling adding the strings, or if you prefer, use String.Concat:

static string Afficher(int[,] m)
{
    string lignePleine = new string('-', m.GetLength(1) * 6 + 1);
    var result = "";
    for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
    {         
        result = string.Concat(result, lignePleine + "\n" + "|");

        for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
        {
            result = string.Concat(result, $"{m[ligne, colonne],3}  |");
        }
        result = string.Concat(result, "\n");
    }
    result = string.Concat(result, lignePleine + '\n');
    return result;
}

You can then call it, for example, using your other function:

public static void Main(string[] args)
{
    Console.WriteLine(Afficher(CreationMatrice()));
}

https://onlinegdb.com/bR2G_UYwb

These are only 2 ways of doing it, there are several, and it depends on target and final goal, here you can find more info on this matter:

Most efficient way to concatenate strings?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

Here it is with the StringBuilder, not much different than the other approaches:

static String Afficher(int[,] m)
{
    string lignePleine = new string('-', m.GetLength(1) * 6 + 1);

    StringBuilder sb = new StringBuilder();
    for (int ligne = 0; ligne < m.GetLength(0); ++ligne)
    {
      sb.AppendLine(lignePleine);
      sb.Append("|");
      for (int colonne = 0; colonne < m.GetLength(1); ++colonne)
      {
        sb.Append($"{m[ligne, colonne],3}  |");
      }
      sb.AppendLine();
    }
    sb.AppendLine(lignePleine);
    sb.AppendLine();
    return sb.ToString();
}
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40