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
?
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 ...
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.
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.
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.
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.
This works for me:
var strArray = new string[] {"abc","def","asd" };
strArray.ToList().ForEach(Console.WriteLine);
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));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With