C# (Chaîne De Caractères.StartsWith && !Chaîne de caractères.EndsWith && !Chaîne de caractères.Contient) à l'aide d'une Liste
Je suis un débutant en C#, et je vais avoir des problèmes.
J'ai 2 Liste 2 cordes et un getCombinations(string) méthode
retourne toutes les combinaisons d'une chaîne de caractères sous forme de Liste;
Comment puis-je valider si un subjectStrings élément n'
StartWith && !EndsWith && !Contient (ou de !StartWith && !EndsWith && Contient, etc.)
pour toutes les combinaisons de startswithString, endswithString et containsString?
Voici mon code dans StartWith && !EndsWith
(si vous voulez le voir en cours d'exécution: 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;
}
}
De sortie:
con
cot
cone
conn
cote <<<
conte <<<
concent
connect
Done
si(z.StartsWith () && !z.EndsWith(b))
var b peut être "et" et "te", mais la côte et conte endswith "te",
pourquoi il est tout de même ajouté dans mes chaînes validées?
Merci à l'avance.
OriginalL'auteur Nikko Guevarra Cabang | 2013-10-13
Vous devez vous connecter pour publier un commentaire.
de vérifier ci-dessous combinaison
donc z commencer par "co" et z sans fin avec "te", votre condition pass et
cote
va ajouter à la listeje voudrais essayer comme ci-dessous
DÉMO
de sortie :
OriginalL'auteur Damith
vous êtes ici paramètre valide pour vrai dès qu'il y a match !z.EndsWith(b) et vous n'êtes pas traversant l'ensemble de la liste des permutations disponibles.
depuis "la côte" ne prend pas fin avec "et" c'est un match et valide est définie sur true et le code des pauses.
C'est pourquoi "la côte" est ajouté à votre liste de chaînes valides. C'est le cas avec "conte".
Ce que vous voulez faire est :
OriginalL'auteur Sameer