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

16,367

Solution 1

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

check below combination

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

so z start with "co" and z not end with "te", your condition pass and cote will add to list

i would try as below

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

output :

con
cot
cone
conn
concent
connect

Solution 2

                            foreach(var b in getCombinations(endswithString))
                        {
                            if(z.StartsWith(a) && !z.EndsWith(b))
                            {
                                    valid = true;
                                    break;
                            }
                        }

here you are setting valid to true as soon as there is match for !z.EndsWith(b) and you are not traversing the whole list of permutations available. since "cote" doesn't end with "et" it is a match and valid is set to true and the code breaks. So that's why "cote" is added to your list of valid strings. So is the case with "conte".

What you want to do is :

    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);
            }
        }
    }
Share:
16,367

Related videos on Youtube

Nikko Guevarra Cabang
Author by

Nikko Guevarra Cabang

Updated on September 18, 2022

Comments

  • Nikko Guevarra Cabang
    Nikko Guevarra Cabang about 1 year

    I am a newbie in C#, and I am having problems.

    I have 2 List, 2 strings and a getCombinations(string) method that returns all combinations of a string as List;

    How do i validate if a subjectStrings element does not StartWith && !EndsWith && !Contains (or !StartWith && !EndsWith && Contains, etc.) for every combinations of startswithString, endswithString and containsString?

    Here is my code in StartWith && !EndsWith (if you want to see it running: 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;
                }
        }
    

    Output:

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

    if(z.StartsWith(a) && !z.EndsWith(b)) var b can be "et" and "te", but cote and conte endswith "te", why it is still added in my validated strings?

    Thanks in advance.

  • Nikko Guevarra Cabang
    Nikko Guevarra Cabang about 10 years
    this worked well in this situation, then tried using it with permutations of "aa" to another List<string> , it doesn't filter invalid strings, found out that my Permutation class doesn't produce any combinations with "aa", so I tried replacing my combinations generator with codeproject.com/Articles/26050/… then it worked perfectly.