您好我有一个很长的字符串。我想将它拆分为某种具有许多返回卡片的格式。 每行有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',',例如在the example中。但它可能有点笨拙。
感谢您的建议。
答案 0 :(得分:2)
如果我理解你,你想要
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 );
答案 1 :(得分:0)
最简单的方法是遵循@Tim删除的方法(编辑:不再删除)答案:
string.Split
)类似的东西(未经测试):
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('";');
答案 2 :(得分:0)
做这样的事情:
string[] words = input.Split(',');
int wordsInString = words.Length;
答案 3 :(得分: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();
答案 4 :(得分: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);
}