Tse*_*iev 5 c# letter line cpu-word
using System;
class HelloCSharp
{
static void Main()
{
Console.WriteLine("Hello C#");
}
}
Run Code Online (Sandbox Code Playgroud)
我希望输出为:
H
e
l
l
o
C
#
Run Code Online (Sandbox Code Playgroud)
但每封信都应该从一个新的行开始
我是新的我知道,但我一直在寻找,找不到答案.它应该是什么Environment.NewLine?
Lut*_*kwa 11
干得好:
string str = "Hello C#"
char[] arr = str.ToCharArray();
foreach (char c in arr)
{
Console.WriteLine(c);
}
Run Code Online (Sandbox Code Playgroud)
通过Join方法实现:
var text = "Hello C#".ToCharArray();
var textInLines = string.Join("\n", text);
Console.WriteLine(textInLines);
Run Code Online (Sandbox Code Playgroud)
编写一个循环遍历字符串的函数.像这样:
void loopThroughString(string loopString)
{
foreach (char c in loopString)
{
Console.WriteLine(c);
}
}
Run Code Online (Sandbox Code Playgroud)
现在你可以调用这个函数:
loopThroughString("Hello c#");
Run Code Online (Sandbox Code Playgroud)
编辑
当然,如果您喜欢linq,您可以将字符串转换为单字符字符串列表,并通过在每个字符之间添加新行来合并它,然后在控制台上打印它
string myString = "Hello c#";
List<string> characterList = myString.Select(c => c.ToString()).ToList();
Console.WriteLine(string.Join("\n", characterList));
Run Code Online (Sandbox Code Playgroud)