C #: I want each letter of the word to start on a new line

using System; class HelloCSharp { static void Main() { Console.WriteLine("Hello C#"); } } 

I want the output to be:

 H e l l o C # 

but each letter must begin with a new line

I am a newbie, but I keep searching and cannot find an answer. Should it be with Environment.NewLine ?

+5
source share
5 answers

Here you go:

 string str = "Hello C#" char[] arr = str.ToCharArray(); foreach (char c in arr) { Console.WriteLine(c); } 
+11
source

Write a function to scroll the line. For instance:

 void loopThroughString(string loopString) { foreach (char c in loopString) { Console.WriteLine(c); } } 

Now you can call this function:

 loopThroughString("Hello c#"); 

EDIT

Of, if you like linq, you can turn a string into a list of one-character strings and concatenate it by adding new lines between each character rather than printing to the console

 string myString = "Hello c#"; List<string> characterList = myString.Select(c => c.ToString()).ToList(); Console.WriteLine(string.Join("\n", characterList)); 
+5
source

Implementation Attach Method:

 var text = "Hello C#".ToCharArray(); var textInLines = string.Join("\n", text); Console.WriteLine(textInLines); 
+5
source

Thanks to everyone, but all the options you have indicated look a little more complicated. Isn't it that simpler:

 const string world = "Hello World!"; for ( int i = 0; i < world.Length; i++) { Console.WriteLine(world[i]); } 

I just ask because I just started to study and is not the most efficient and fastest way to write a program the best? I know that they have many ways to make something work.

+1
source

Real men use regular expressions for everything! :-)

 string str = "Hello\nC#"; string str2 = Regex.Replace(str, "(.)", "$1\n", RegexOptions.Singleline); Console.Write(str2); 

This regular expression searches for any character (.) And replaces it with the found character plus a \n ( $1\n )

(no, please ... this is wrong ... you shouldn't use regular expressions in C # unless you are really desperate).

0
source

All Articles