C# 将一个逗号分隔的字符串转置到另一个逗号分隔的字符串上



我想将值从一个逗号分隔的字符串转置到另一个逗号分隔的字符串。

例 1

Input 1 : 1,19,2,20,3,30
Input 2 : ,,{0},{1},,
Output  : ,,2,20,,

例 2

Input 1 : 1,19,2,20,3,30
Input 2 : ,,,,{0},{1}
Output  : ,,,,3,30

两个输入字符串在开头都有一些其他值。因此,如果有任何从末端计算头寸的逻辑对我有益。

我实际上有一个字符串列表,其中包含输入 2 个值等模式。然后输入 1 值需要转置到所有具有类似输入 2 模式的字符串。

谢谢 洛汗国。

你可以做这样的事情:

void Main()
{
Console.WriteLine(
TransformCustom("1,19,2,20,3,30",",,{ 0},{ 1},,")
);
//Output: ,,2,20,,
Console.WriteLine(
TransformCustom("1,19,2,20,3,30",",,,,{ 0},{ 1}")
);
//Output: ,,,,3,30
}
private string TransformCustom(string input1, string input2)
{
return string.Join(",",
input1.Split(',').Zip(input2.Split(','), (i1, i2) => new {i1, i2})
.Select(i => string.IsNullOrEmpty(i.i2)?"":i.i1));
}

最新更新