C#(String.StartsWith&;!String.EndsWith&;!String.Contains)使用列表
我是C#的新手,我有问题 我有2个List、2个string和一个getCombinations(string)方法 以列表形式返回字符串的所有组合 如果subjectStrings元素不存在,如何进行验证 开始&&!结束&&!包含(或!StartWith&&!EndsWith&&Contains等) 对于startswithString、endswithString和containsString的每个组合 这是我在StartWith&&中的代码!EndsWith (如果希望看到它运行:)C#(String.StartsWith&;!String.EndsWith&;!String.Contains)使用列表,c#,string,linq,combinations,startswith,C#,String,Linq,Combinations,Startswith,我是C#的新手,我有问题 我有2个List、2个string和一个getCombinations(string)方法 以列表形式返回字符串的所有组合 如果subjectStrings元素不存在,如何进行验证 开始&&!结束&&!包含(或!StartWith&&!EndsWith&&Contains等) 对于startswithString、endswithString和containsString的每个组合 这是我在StartWith&&中的代码!EndsWith (如果希望看到它运行:) 使用
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公开课考试
{
公共静态void Main()
{
List validatedStrings=新列表();
列表主题字符串=新列表()
{
“con”、“cot”、“eon”、“net”、“not”、“one”、“ten”、“toe”、“ton”,
“分”、“圆锥体”、“conn”、“cote”、“霓虹灯”、“无”、“音符”、“一次”、“音调”,
“cento”、“conte”、“nonce”、“nonet”、“oncet”、“tenon”、“tone”,
“nocent”、“concent”、“connect”
};//有更长的单词列表
字符串startString=“co”;
字符串endswithString=“et”;
foreach(subjectStrings中的var z)
{
bool valid=false;
foreach(getCombinations中的var a(startswithString))
{
foreach(getcompositions中的变量b(endswithString))
{
如果(z.StartsWith(a)和&!z.EndsWith(b))
{
有效=真;
打破
}
}
如果(有效)
{
打破
}
}
如果(有效)
{
validatedStrings.Add(z);
}
}
foreach(validatedStrings中的var a)
{
控制台写入线(a);
}
Console.WriteLine(“\n目录”);
}
静态列表组合(字符串s)
{
//计算组合的代码
返回置换。置换;
}
}
公共类置换
{
私有静态列表所有组合;
私有静态void calculate组合(字符串字、列表临时值)
{
if(temp.Count==字长)
{
List clone=temp.ToList();
if(clone.Distinct().Count()==clone.Count)
{
添加(克隆);
}
返回;
}
for(int i=0;i
输出:
con
cot
cone
conn
cote <<<
conte <<<
concent
connect
Done
con
婴儿床
圆锥
康涅狄格州
科特
检查下面的组合
z ="cote"
a ="co"
b ="te"
所以z以“co”开始,而不是以“te”结束,您的条件通过,cote将添加到列表中
我会尝试如下
var sw =getCombinations(startswithString);
var ew = getCombinations(endswithString);
var result = subjectStrings.Where(z=>
sw.Any(x=>z.StartsWith(x) &&
!ew.Any(y=>z.EndsWith(y))))
.ToList();
输出:
con
cot
cone
conn
concent
connect
在这里,只要有匹配项,您就可以将valid设置为true!z、 EndsWith(b)并且您没有遍历整个可用排列列表。
由于“cote”不以“et”结尾,因此它是一个匹配项,valid设置为true,代码中断。
这就是为什么“cote”被添加到有效字符串列表中的原因。“conte”也是如此
您要做的是:
List<string> startsWithCombination = getCombinations("co");
List<string> endsWithCombination = getCombinations("et");
foreach (var z in subjectStrings)
{
bool isStartMatchFound = startsWithCombination.Any(b => z.StartsWith(b));
if (isStartMatchFound)
{
bool isEndMatchFound = endsWithCombination.Any(b => z.EndsWith(b));
if (!isEndMatchFound)
{
validatedStrings.Add(z);
}
}
}
List startsWithCombination=getCombinations(“co”);
列表endsWithCombination=getCombinations(“et”);
foreach(subjectStrings中的var z)
{
bool isStartMatchFound=startswithcomposition.Any(b=>z.StartsWith(b));
如果(已找到)
{
bool isEndMatchFound=endswithcomposition.Any(b=>z.EndsWith(b));
如果(!isEndMatchFound)
{
validatedStrings.Add(z);
}
}
}
对于此类问题,您可以使用递归函数。请提供更多示例,让我确切了解函数应作为输出返回的内容。这在这种情况下效果很好,然后尝试将其与“aa”的排列一起使用到另一个列表,它不会过滤无效字符串,我发现我的置换类不产生任何带有“aa”的组合,所以我尝试用它来替换我的组合生成器,然后它工作得很好。
con
cot
cone
conn
concent
connect
foreach(var b in getCombinations(endswithString))
{
if(z.StartsWith(a) && !z.EndsWith(b))
{
valid = true;
break;
}
}
List<string> startsWithCombination = getCombinations("co");
List<string> endsWithCombination = getCombinations("et");
foreach (var z in subjectStrings)
{
bool isStartMatchFound = startsWithCombination.Any(b => z.StartsWith(b));
if (isStartMatchFound)
{
bool isEndMatchFound = endsWithCombination.Any(b => z.EndsWith(b));
if (!isEndMatchFound)
{
validatedStrings.Add(z);
}
}
}