Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Print list of string array

Tags:

arrays

c#

list

How do I print a list of string arrays? I can do it from string[] using Console.WriteLine, but if I do that for a list with foreach it just prints out System.String[]. How do I write an index when using a foreach?

like image 589
atilas1 Avatar asked Feb 08 '17 11:02

atilas1


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


4 Answers

The simplest way to achieve this is: using String.Join

string[] arr = new string[] { "one", "two", "three", "four" };
Console.WriteLine(String.Join("\n", arr)); 

Hope this helps.

like image 119
PulkitG Avatar answered Oct 17 '22 23:10

PulkitG


This works for me:

var strArray = new string[] {"abc","def","asd" };
strArray.ToList().ForEach(Console.WriteLine);
like image 31
master2080 Avatar answered Oct 17 '22 22:10

master2080


So you have list of string arrays, like this:

 List<string[]> data = new List<string[]>() {
   new string[] {"A", "B", "C"},
   new string[] {"1", "2"},
   new string[] {"x", "yyyy", "zzz", "final"},
 };

To print on, say, the Console, you can implement nested loops:

 foreach (var array in data) {
   Console.WriteLine();

   foreach (var item in array) {
     Console.Write(" ");
     Console.Write(item); 
   }
 }

Or Join the items into the single string and then print it:

 using System.Linq;
 ...

 string report = string.Join(Environment.NewLine, data
   .Select(array => string.Join(" ", array)));

 Console.Write(report);

Or combine both methods:

 foreach (var array in data) 
   Console.WriteLine(string.Join(" ", array));
like image 34
Dmitry Bychenko Avatar answered Oct 17 '22 23:10

Dmitry Bychenko


string[] arr = new string[2]{"foo","zoo"}; // sample Initialize.

// Loop over strings.
foreach (string s in arr)
{
    Console.WriteLine(s);
}

The console output:

foo
zoo
like image 2
user114649 Avatar answered Oct 17 '22 23:10

user114649