C# String.StartsWith && !String.EndsWith && !String.Enthält) unter Verwendung einer Liste
Ich bin ein Neuling in C#, und ich habe Probleme.
Ich habe 2 Liste, 2 strings und ein getCombinations(string) - Methode, die
gibt alle Kombinationen einer string-Liste;
Wie kann ich überprüfen, ob ein subjectStrings element nicht
StartWith && !EndsWith && !Enthält (oder !StartWith && !EndsWith && Enthält, etc.)
für alle Kombinationen von startswithString, endswithString und containsString?
Hier ist mein code in StartWith && !EndsWith
(wenn Sie sehen wollen, es läuft: http://ideone.com/y8JZkK)
using System;
using System.Collections.Generic;
using System.Linq;
public class Test
{
public static void Main()
{
List<string> validatedStrings = new List<string>();
List<string> subjectStrings = new List<string>()
{
"con", "cot", "eon", "net", "not", "one", "ten", "toe", "ton",
"cent", "cone", "conn", "cote", "neon", "none", "note", "once", "tone",
"cento", "conte", "nonce", "nonet", "oncet", "tenon", "tonne",
"nocent","concent", "connect"
}; //got a more longer wordlist
string startswithString = "co";
string endswithString = "et";
foreach(var z in subjectStrings)
{
bool valid = false;
foreach(var a in getCombinations(startswithString))
{
foreach(var b in getCombinations(endswithString))
{
if(z.StartsWith(a) && !z.EndsWith(b))
{
valid = true;
break;
}
}
if(valid)
{
break;
}
}
if(valid)
{
validatedStrings.Add(z);
}
}
foreach(var a in validatedStrings)
{
Console.WriteLine(a);
}
Console.WriteLine("\nDone");
}
static List<string> getCombinations(string s)
{
//Code that calculates combinations
return Permutations.Permutate(s);
}
}
public class Permutations
{
private static List<List<string>> allCombinations;
private static void CalculateCombinations(string word, List<string> temp)
{
if (temp.Count == word.Length)
{
List<string> clone = temp.ToList();
if (clone.Distinct().Count() == clone.Count)
{
allCombinations.Add(clone);
}
return;
}
for (int i = 0; i < word.Length; i++)
{
temp.Add(word[i].ToString());
CalculateCombinations(word, temp);
temp.RemoveAt(temp.Count - 1);
}
}
public static List<string> Permutate(string str)
{
allCombinations = new List<List<string>>();
CalculateCombinations(str, new List<string>());
List<string> combinations = new List<string>();
foreach(var a in allCombinations)
{
string c = "";
foreach(var b in a)
{
c+=b;
}
combinations.Add(c);
}
return combinations;
}
}
Ausgabe:
con
cot
cone
conn
cote <<<
conte <<<
concent
connect
Done
if(z.StartsWith(a) && !z.EndsWith(b))
var b sein kann, "et" und "te", aber cote und conte endswith "te",
warum ist es immer noch in meinem validiert strings?
Vielen Dank im Voraus.
InformationsquelleAutor Nikko Guevarra Cabang | 2013-10-13
Du musst angemeldet sein, um einen Kommentar abzugeben.
prüfen Sie die unten stehende Kombination
also z beginnen Sie mit "co" und z enden nicht mit "te", Ihr Zustand übergeben und
cote
wird zu der Liste hinzufügenich würde versuchen, wie unten
DEMO
Ausgabe :
InformationsquelleAutor Damith
hier sind Sie, gültig, wahr, sobald es ist match für !z.EndsWith(b) und Sie sind nicht das Durchlaufen der gesamten Liste von Permutationen zur Verfügung.
da "cote" endet nicht mit "et" es ist ein Spiel und gültig ist, ist festgelegt auf true und der code bricht.
So, dass ist der Grund, warum "cote" ist Hinzugefügt, um Ihre Liste Gültiger Zeichenketten. So ist der Fall mit "conte".
Was Sie tun möchten, ist :
InformationsquelleAutor Sameer