本文介绍了逗号分隔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到一个像
50'FRAME CABLE 2W(6X)50'FRAME CABLE 3W(9X)
但是我想把它显示为50'FRAME CABLE(2W(6X),3W(9X))
在这一行我是获得价值
I am getting a string like
50' FRAME CABLE 2W (6X)50' FRAME CABLE 3W (9X)
but i want to show it like 50' FRAME CABLE (2W (6X),3W (9X))
In this line i am getting the value
StringBuilder terminalBoxSummary = GetFrameCableSensorCableSummary(terminalBoxs);
private static StringBuilder GetFrameCableSensorCableSummary(List<SageControlBase> igus_Sensors)
{
var frameCables2WCount = igus_Sensors.Where(x => ((x as IGUs) != null && (x as IGUs).BusBars == SageConstants.BusBarsTypes.Two));
var frameCables3WCount = igus_Sensors.Where(x => ((x as IGUs) != null && (x as IGUs).BusBars == SageConstants.BusBarsTypes.Three));
var frameCables4WCount = igus_Sensors.Where(x => ((x as IGUs) != null && (x as IGUs).BusBars == SageConstants.BusBarsTypes.Four));
var sensorcables2WCount = igus_Sensors.Where(x => ((x as Sensors) != null && (x as Sensors).BusBars == SageConstants.BusBarsTypes.Two));
var sensorcables3WCount = igus_Sensors.Where(x => ((x as Sensors) != null && (x as Sensors).BusBars == SageConstants.BusBarsTypes.Three));
var sensorcables4WCount = igus_Sensors.Where(x => ((x as Sensors) != null && (x as Sensors).BusBars == SageConstants.BusBarsTypes.Four));
StringBuilder frameCableSummary = new StringBuilder();
foreach (var group in frameCables2WCount.GroupBy(x => (x as IGUs).FrameCableLength))
{
if (group.Count() > 0)
frameCableSummary.AppendLine(string.Format("{0}' FRAME CABLE 2W ({1}X)", (group.FirstOrDefault() as IGUs).FrameCableLength.ToString(), group.Count().ToString()));
}
foreach (var group in frameCables3WCount.GroupBy(x => (x as IGUs).FrameCableLength))
{
if (group.Count() > 0)
frameCableSummary.AppendLine(string.Format("{0}' FRAME CABLE 3W ({1}X)", (group.FirstOrDefault() as IGUs).FrameCableLength.ToString(), group.Count().ToString()));
}
foreach (var group in frameCables4WCount.GroupBy(x => (x as IGUs).FrameCableLength))
{
if (group.Count() > 0)
frameCableSummary.AppendLine(string.Format("{0}' FRAME CABLE 4W ({1}X)", (group.FirstOrDefault() as IGUs).FrameCableLength.ToString(), group.Count().ToString()));
}
foreach (var group in sensorcables2WCount.GroupBy(x => (x as Sensors).FrameCableLength))
{
if (group.Count() > 0)
frameCableSummary.AppendLine(string.Format("{0}' SENSOR CABLE 2W ({1}X)", (group.FirstOrDefault() as Sensors).FrameCableLength.ToString(), group.Count().ToString()));
}
foreach (var group in sensorcables3WCount.GroupBy(x => (x as Sensors).FrameCableLength))
{
if (group.Count() > 0)
frameCableSummary.AppendLine(string.Format("{0}' SENSOR CABLE 3W ({1}X)", (group.FirstOrDefault() as Sensors).FrameCableLength.ToString(), group.Count().ToString()));
}
foreach (var group in sensorcables4WCount.GroupBy(x => (x as Sensors).FrameCableLength))
{
if (group.Count() > 0)
frameCableSummary.AppendLine(string.Format("{0}' SENSOR CABLE 4W ({1}X)", (group.FirstOrDefault() as Sensors).FrameCableLength.ToString(), group.Count().ToString()));
}
return frameCableSummary;
}
推荐答案
private string dString = @"50' FRAME CABLE 2W (6X)50' FRAME CABLE 3W (9X) 50' FRAME CABLE 4W (9X)";
private string fancyOutput(string source)
{
string[] parse = source.Split(new string[] {"FRAME CABLE"}, StringSplitOptions.RemoveEmptyEntries);
StringBuilder sb;
if (parse.Length >= 2)
{
string first = parse[0];
sb = new StringBuilder(first + "Frame Cable (");
for (int i = 1; i < parse.Length; i++)
{
sb.Append(parse[i].Replace(first, ""));
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);
sb.Append(")");
string result = sb.ToString();
// for debugging only
Console.WriteLine(result());
return result;
}
else
{
return "something went wrong: check input string";
}
}
这篇关于逗号分隔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!