c# - 将长字符串拆分为自定义字符串

标签 c# regex linq

你好,我有一个很长的字符串。我想将其分割为某种具有许多回程的格式。 每行有 5 个短单词。 例如。

字符串输入=“'250.0','250.00','250.01','250.02','250.03','250.1','250.10','250.11','250.12','250.13', '250.2','250.20','250.21','250.22','250.23','250.3','250.30','250.31','250.32','250.33','250.4','250.40','250.41 ','250.42','250.43','250.5','250.50','250.51','250.52','250.53','250.6','250.60','250.61','250.62','250.63', '250.7','250.70','250.71','250.72','250.73','250.8','250.80','250.81','250.82','250.83','250.9','250.90','250.91 ','250.92','250.93','357.2','357.20','362.01','362.02','362.03','362.04','362.05','362.06','362.07','366.41', '648.0','648.00','648.01','648.02','648.03','648.04'";

它有 66 个短单词。

string output = "'250.0','250.00','250.01','250.02','250.03',
                 '250.1','250.10','250.11','250.12','250.13',
                 '250.2','250.20','250.21','250.22','250.23',
                 '250.3','250.30','250.31','250.32','250.33',
                 '250.4','250.40','250.41','250.42','250.43',
                 '250.5','250.50','250.51','250.52','250.53',
                 '250.6','250.60','250.61','250.62','250.63',
                 '250.7','250.70','250.71','250.72','250.73',
                 '250.8','250.80','250.81','250.82','250.83',
                 '250.9','250.90','250.91','250.92','250.93',
                 '357.2','357.20','362.01','362.02','362.03',
                 '362.04','362.05','362.06','362.07','366.41',
                 '648.0','648.00','648.01','648.02','648.03',
                 '648.04'";

我认为我必须首先计算字符串中的字符“,”,例如 the example 。但这可能有点笨拙。

感谢您的建议。

最佳答案

如果我正确理解了你的意思,你想

  • 用逗号分隔这些单词
  • 将结果分组为每行包含 5 个单词的行
  • 构建一个以 Environment.NewLine 作为分隔符的字符串

string input = "'250.0','250.00','250.01','250.02','250.03','250.1','250.10','250.11','250.12','250.13','250.2','250.20','250.21','250.22','250.23','250.3','250.30','250.31','250.32','250.33','250.4','250.40','250.41','250.42','250.43','250.5','250.50','250.51','250.52','250.53','250.6','250.60','250.61','250.62','250.63','250.7','250.70','250.71','250.72','250.73','250.8','250.80','250.81','250.82','250.83','250.9','250.90','250.91','250.92','250.93','357.2','357.20','362.01','362.02','362.03','362.04','362.05','362.06','362.07','366.41','648.0','648.00','648.01','648.02','648.03','648.04'";
int groupCount = 5; 
var linesGroups = input.Split(',')
    .Select((s, index) => new { str = s, Position = index / groupCount, Index = index })
    .GroupBy(x => x.Position);

StringBuilder outputBuilder = new StringBuilder();
foreach (var grp in linesGroups)
{
    outputBuilder.AppendLine(String.Join(",", grp.Select(x => x.str)));
}
String output = outputBuilder.ToString();

编辑:结果是:

'250.0','250.00','250.01','250.02','250.03'
'250.1','250.10','250.11','250.12','250.13'
'250.2','250.20','250.21','250.22','250.23'
'250.3','250.30','250.31','250.32','250.33'
'250.4','250.40','250.41','250.42','250.43'
'250.5','250.50','250.51','250.52','250.53'
'250.6','250.60','250.61','250.62','250.63'
'250.7','250.70','250.71','250.72','250.73'
'250.8','250.80','250.81','250.82','250.83'
'250.9','250.90','250.91','250.92','250.93'
'357.2','357.20','362.01','362.02','362.03'
'362.04','362.05','362.06','362.07','366.41'
'648.0','648.00','648.01','648.02','648.03'
'648.04'

如果您想在每一行后添加逗号(如您的示例中所示):

foreach (var grp in linesGroups)
{
    outputBuilder.AppendLine(String.Join(",", grp.Select(x => x.str)) + ",");
}
// remove last comma + Environment.NewLine
outputBuilder.Length -= ( 1 + Environment.NewLine.Length );

关于c# - 将长字符串拆分为自定义字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10500626/

相关文章:

c# - 何时使用 javascript 而不是创建 aspx 页面?

c# - 有没有办法将 RectTransform 传输到 unity3d 中的相机 ViewPort?

android - 正则表达式模式去除一些无效的 JSON 元素

java - 如何用java正则表达式表达这个模式?

c# - 如何在 C# 中将文本值拆分为包含单词的数组?

c# - 为什么有些对象不能从不同的线程访问?

java - 使用正则表达式获取特殊字符之前的特定单词

c# - 使用 Lambda 表达式进行加入和分组

c# - SQLite 按位查询到 EF Core Linq 版本?

c# - 如何在C#中实现列表的惰性洗牌?