这应该可以满足您的需求。关键是创建一个列表列表并在原始数组中的每个项目上拆分换行符。
这应该适用于任意数量的字符串,但每个字符串必须具有相同数量的换行符才能中断。
例如:
"Yo \nDon't kill \nMan!"
"dude! \nme bro! "
这将导致异常,因为第二行只有一个\n
要解决此问题,可以将第二行更改为:
"Yo \nDon't kill \nMan!"
"dude! \nme bro! \n"
这应该为您提供正确的格式。
在 VB 中:
Imports System.Text
Module Module1
Sub Main()
Dim items As List(Of String) = New List(Of String)()
items.Add("My dog\nyou")
items.Add(" dont like that\n")
items.Add(" ... \n will die!")
Dim list As List(Of List(Of String)) = New List(Of List(Of String))
Dim arg() As String = {"\n"}
For Each listItem As String In items
list.Add(listItem.Split(arg, StringSplitOptions.None).ToList())
Next
Dim sb As StringBuilder = New StringBuilder()
For i As Integer = 0 To list(0).Count - 1
For j As Integer = 0 To list.Count - 1
sb.Append(list(j)(i))
Next
sb.Append(Environment.NewLine)
Next
Console.WriteLine(sb.ToString())
Console.ReadKey()
End Sub
End Module
和 C#(我的偏好,但这一切都很好:)):
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> items = new List<string>();
items.Add("My dog\nyou");
items.Add(" dont like that\n");
items.Add(" ... \n will die!");
List<List<string>> list = new List<List<string>>();
items.ForEach(f => list.Add(f.Split('\n').ToList()));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list[0].Count; i++)
{
for (int j = 0; j < list.Count; j++)
{
sb.Append(list[j][i]);
}
sb.Append(Environment.NewLine);
}
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
}
}