c#字符串拆分和合并
我有一个包含5个数字的字符串c#字符串拆分和合并,c#,arrays,string,combinations,C#,Arrays,String,Combinations,我有一个包含5个数字的字符串 '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' 简单/快速的方法是什么 每次不同的输入和组合时,是否将其转换为未设置的数组 把它们放回弦上 有简单的方法吗 感谢使用string.Split()您可以创建字符串数组。循环遍历它,以便在每个循环迭代中指示应该跳过哪个元素(在第一次循环中,
'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'
简单/快速的方法是什么
每次不同的输入和组合时,是否将其转换为未设置的数组
把它们放回弦上
有简单的方法吗
感谢使用string.Split()
您可以创建字符串数组。循环遍历它,以便在每个循环迭代中指示应该跳过哪个元素(在第一次循环中,忽略第一个元素,在第二次循环中,忽略第二个元素)
在该循环中,创建一个新数组,该数组包含除要跳过的元素外的所有元素,然后使用string.Join()
创建每个结果。查看:
在这里,你会发现F#中的一个例子,他将在组合和排列中给出正确的背景(这就是所谓的“你所需要的”)。还有代码,我认为用C语言翻译很容易#
像这样的东西怎么样
string s = "1,4,14,32,47";
string r = String.Join(",", s.Split(',').Where((x, index) => index != 1).ToArray());
你的措词很混乱……但例子很清楚。只需将其放在逗号上,然后是一个索引,然后使用string.Join(“,”,list);对于那些需要更通用算法的人来说,这里有一个算法可以给出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);
}
}
private void GetPermutations()
{
int置换长度=4;
字符串s=“1,4,14,32,47”;
字符串[]subS=s.Split(',');
int[]indexS=新的int[置换长度];
列表结果=新列表();
IterateNextPerm(排列长度、索引、子项、结果);
//结果将保存您的所有生成数据。
}
私有void IterateNextPerm(int排列长度,int[]索引,string[]子,列表结果)
{
int maxIndexValue=subS.Count()-1;
bool isCorrect=true;
对于(int index=0;index=pIndexes[index+1])
{
isCorrect=false;
打破
}
}
//如果正确,打印结果
如果(不正确)
{
string strnewpermutate=string.Empty;
for(int index=0;indexmaxIndexValue)
{
int?lastIndexIncreased=null;
//后退一步,增加索引
对于(int索引=排列长度-1;索引>0;索引--)
{
if(pIndexes[index]>maxIndexValue)
{
指数[指数-1]++;
lastIndexIncreased=索引-1;
}
}
//规范化索引数组,以防止不必要的步骤
如果(lastIndexIncreased!=null)
{
for(int索引=(int)LastIndexIncreded+1;index你不是说…index!=x…
?不,index是数组中的索引。0..4看起来不错,我从0-4运行第2行,每次都得到正确的字符串,我怀疑你的第4行中有一个类型。它应该是“1,14,32,47”,而不是“1,4,32,47”。很好!+1因为我认识到他实际上是在尝试使用ge生成字符串中字符的置换。
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();