我有一个字符串,我想在每第二个逗号之后拆分该字符串。在 C# 中使用拆分字符串是否可行?
示例字符串:
"This,is,an, example,for,the,stackoverflow,community"
期望的输出
This,is
an,example
for,the
stackoverflow,community
如有任何帮助,我们将不胜感激!
最佳答案
使用 Enumerable.Chunk从 .NET 6 开始,您可以
- 拆分
","
, - 分别创建两个项目的 block
- 重新加入这些 block :
string input = "This,is,an,example,for,the,stackoverflow,community";
var output = input.Split(",")
.Chunk(2)
.Select(chunk => string.Join(",", chunk));
foreach (string s in output)
Console.WriteLine(s);
如果您坚持使用“经典”.NET Framework,这里是 .NET < 6 的 block 实现:
关于c# - 如何在每第 n 个字符后拆分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71373986/