簡體   English   中英

使用List的C#(String.StartsWith &&!String.EndsWith &&!String.Contains)

[英]C# (String.StartsWith && !String.EndsWith && !String.Contains) using a List

我是C#的新手,我遇到了問題。

我有2個List,2個字符串和一個getCombinations(字符串)方法,它返回字符串的所有組合作為List;

我如何驗證一個subjectStrings元素是否沒有StartWith &&!EndsWith &&!包含(或者!StartWith &&!EndsWith && Contains等)每個startswithString,endswithString和containsString的組合?

這是我的代碼在StartWith &&!EndsWith(如果你想看到它運行: 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;
            }
    }

輸出:

    con 
    cot
    cone
    conn
    cote <<<
    conte <<<
    concent
    connect

    Done

if(z.StartsWith(a)&&!z.EndsWith(b))var b可以是“et”和“te”,但cote和conte以“te”結尾,為什么它仍然添加在我的驗證字符串中?

提前致謝。

z.StartsWith(a) && !z.EndsWith(b)

檢查下面的組合

z ="cote"
a ="co"
b ="te"

所以z以“co”開頭而z不以“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();

DEMO

輸出:

con
cot
cone
conn
concent
connect
                            foreach(var b in getCombinations(endswithString))
                        {
                            if(z.StartsWith(a) && !z.EndsWith(b))
                            {
                                    valid = true;
                                    break;
                            }
                        }

只要匹配!z.EndsWith(b)並且您沒有遍歷可用的整個排列列表,就在這里設置有效為true。 因為“cote”不以“et”結尾,所以它是匹配,有效設置為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);
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM