我有conatin 5号像C#字符串分割和组合
'1,4,14,32,47'
我想从这个字符串进行5串4号字符串中的每一个
,如:
'1,4,14,32'
'1,4,14,47'
'1,4,32,47'
'1,14,32,47'
'4,14,32,47'
什么是简单/快速的方法做到这一点
是将其转换为数组每次未设置的方式不同的entery和结合 他们回到字符串?
有没有简单的方法来做到这一点?
感谢
我有conatin 5号像C#字符串分割和组合
'1,4,14,32,47'
我想从这个字符串进行5串4号字符串中的每一个
,如:
'1,4,14,32'
'1,4,14,47'
'1,4,32,47'
'1,14,32,47'
'4,14,32,47'
什么是简单/快速的方法做到这一点
是将其转换为数组每次未设置的方式不同的entery和结合 他们回到字符串?
有没有简单的方法来做到这一点?
感谢
如何像
string s = "1,4,14,32,47";
string r = String.Join(",", s.Split(',').Where((x, index) => index != 1).ToArray());
你是不是指`... index!= x ...`? – 2010-12-10 08:34:55
使用string.Split()
你可以创建一个字符串数组。循环遍历它,以便在每次循环迭代中指示应该跳过哪个元素(在第一遍时忽略第一个元素,在第二遍时忽略第二个元素)。
在该循环中,创建一个新数组,其中包含除跳过的所有元素,然后使用string.Join()
创建每个结果。
看一看:
http://msdn.microsoft.com/en-us/magazine/ee310028.aspx在这里,你会发现在F#谁给的组合和排列正确的背景为例(这是怎么叫你的需要)。有代码也一样,我觉得它很容易在C#转换
非常好! +1用于识别他实际上是在尝试生成字符串中字符的排列。 – 2010-12-10 08:39:14
这很清楚,队友:D – Mauro 2010-12-10 08:39:49
对于那些谁需要一个更通用的算法,这里是一个赋予的m个n个长度为子集:
private void GetPermutations()
{
int permutationLength = 4;
string s = "1,4,14,32,47";
string[] subS = s.Split(',');
int[] indexS = new int[permutationLength];
List<string> result = new List<string>();
IterateNextPerm(permutationLength, indexS, subS, result);
// Result will hold all your genberated data.
}
private void IterateNextPerm(int permutationLength, int[] pIndexes, string[] subS, List<string> result)
{
int maxIndexValue = subS.Count() - 1;
bool isCorrect = true;
for (int index = 0; index < permutationLength - 1; index++)
{
if (pIndexes[index] >= pIndexes[index + 1])
{
isCorrect = false;
break;
}
}
// Print result if correct
if (isCorrect)
{
string strNewPermutation = string.Empty;
for (int index = 0; index < permutationLength; index++)
{
strNewPermutation += subS[pIndexes[index]] + ",";
}
result.Add(strNewPermutation.TrimEnd(','));
}
// Increase last index position
pIndexes[permutationLength - 1]++;
// Check and fix if it's out of bounds
if (pIndexes[permutationLength - 1] > maxIndexValue)
{
int? lastIndexIncreased = null;
// Step backwards and increase indexes
for (int index = permutationLength - 1; index > 0; index--)
{
if (pIndexes[index] > maxIndexValue)
{
pIndexes[index - 1]++;
lastIndexIncreased = index - 1;
}
}
// Normalize indexes array, to prevent unnecessary steps
if (lastIndexIncreased != null)
{
for (int index = (int)lastIndexIncreased + 1; index <= permutationLength - 1; index++)
{
if (pIndexes[index - 1] + 1 <= maxIndexValue)
{
pIndexes[index] = pIndexes[index - 1] + 1;
}
else
{
pIndexes[index] = maxIndexValue;
}
}
}
}
if (pIndexes[0] < maxIndexValue)
{
IterateNextPerm(permutationLength, pIndexes, subS, result);
}
}
我知道这是不是最好的编码,但我现在在过去的半小时内已经写了,所以我确信有些事情需要在那里进行。
玩得开心编码!
var elements = string.Split(',');
var result =
Enumerable.Range(0,elements.Length)
.Reverse()
.Select(
i=>
string.Join(","
Enumerable.Range(0,i).Concat(Enumerable.Range(i+1,elements.Length - i - 1))
.Select(j=>elements[j]).ToArray() // This .ToArray() is not needed in NET 4
)
).ToArray();
我怀疑你的第4排有一个类型。应该是'1,14,32,47'而不是'1,4,32,47' – 2010-12-10 08:35:38
坦克我修好了吧 – 2010-12-10 08:42:16