2

你好,我有一个长字符串。我想将其拆分为具有许多返回carrages的某种格式。每行有5个简短的单词。前任。

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'";

它有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'";

我认为我必须首先在字符串中计算 char ',' ,例如在示例中。但这可能有点笨拙。

谢谢你的建议。

4

5 回答 5

2

如果我对你的理解正确,你想

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

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 );
于 2012-05-08T14:31:32.497 回答
0

最简单的方法是遵循@Tim 的已删除(编辑:不再删除)答案中的方法:

  1. 用逗号将字符串分成几部分(使用string.Split
  2. 以您需要的任何方式重新排列获得的零件:例如,将 5 个包装成一行。

类似的东西(未测试):

Console.WriteLine("string output =");
var parts = sourceString.Split(',');
int i = 0;
for (; i < parts.Length; i++)
{
    if (i % 5 == 0)
        Console.Write('    "');
    Console.Write(parts[i]);
    Console.Write(',');
    if (i % 5 == 4 && i != parts.Length - 1)
        Console.WriteLine('" +');
}
Console.WriteLine('";');
于 2012-05-08T14:35:37.360 回答
0

做这样的事情:

string[] words = input.Split(','); 
int wordsInString = words.Length;
于 2012-05-08T14:36:21.887 回答
0
var 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'";

var wordsArray = input.Split(',');
var sbOutput = new StringBuilder();

for (int i = 1; i < wordsArray.Length +1; i++)
{
    sbOutput.AppendFormat("{0},", wordsArray[i-1]);

    if(i % 5 == 0)
        sbOutput.AppendLine();
}

var output = sbOutput.ToString();
于 2012-05-08T14:43:15.597 回答
0

这个解决方案怎么样:

private static IEnumerable<string> SplitLongString(string input, char separator, int groupSize)
{
    int indexCurrent = 0;
    int indexLastOccurence = 0;
    int separatorCounter = 0;

    foreach (var character in input)
    {
        indexCurrent++;

        if (character == separator)
        {
            separatorCounter++;

            if (separatorCounter % groupSize == 0)
            {
                yield return input.Substring(indexLastOccurence, indexCurrent - indexLastOccurence);
                indexLastOccurence = indexCurrent;
            }
        }
    }

    if (indexCurrent != indexLastOccurence)
    {
        yield return input.Substring(indexLastOccurence, indexCurrent - indexLastOccurence);
    }
}

你会称之为:

var result = SplitLongString(input, ',', 5);

foreach (var row in result)
{
    Console.WriteLine(row);
}
于 2012-05-08T15:01:06.817 回答