开发者

match partial string in List<string> in another List<string>

开发者 https://www.devze.com 2023-03-22 00:44 出处:网络
its me. i got the following code: foreach (var str in usedCSS) { if (CSS.Any(c => c.IndexOf(str)>0))

its me. i got the following code:

foreach (var str in usedCSS) { 
    if (CSS.Any(c => c.IndexOf(str)>0))        
        Response.Write(str + "<br />"); 
    else        
        Response.Write("Could not find: " + str + "<br />"); 
}

usedCSS = List<string>

CSS = List<string>

but, i need it the other way around...

i want the var str in usedCSS to be var str in CSS

usedCSS contains strings of only the css names e.g: .header

CSS contains string of the actual css e.g: .header {font-size:14px;}

basicly, what i need is to print out the actuall CSS that is 开发者_开发技巧used. The code i currently have does the exact opposite, it returns only the css names, not the actuall css.


If I understand what it is you're trying to do correctly, you could do this. FirstOrDefault() finds the first occurrence of a predicate match and returns it, or null (default for type string) if not found. Then all we need is the StartsWith() string method to match each item in CSS prefix-wise with the str in question.

        foreach (var str in usedCSS)
        {
            // FirstOrDefault finds first match or returns default (null for string) if not found.
            var match = CSS.FirstOrDefault(s => s.StartsWith(str));

            if (match != null)

                // get the match.  
                Response.Write(match + "<br />");
            }
            else
            {
                Response.Write("Could not find: " + str + "<br />");
            }
        }

p.s. The above works if you only expect one or no matches, if you can have multiple matches, and want them all:

foreach (var str in usedCSS)
{
    foreach(var match in CSS.Where(s => s.StartsWith(str)))
    {
        // get the match.  
        Response.Write(match + "<br />");
    }
}


Try

var R = (from str in CSS from x in usedCSS where str.StartsWith (x) select str).ToList();
foreach ( var V in R )
{
    Response.Write ( V.ToString() + "<br />");
} 


        List<string> result = CSS.Where(a => usedCSS.Any(b => b.IndexOf(a) >= 0)).ToList();
        result.ForEach(a => Console.WriteLine(a));

and the reverse

        List<string> result = usedCSS.Where(a => CSS.Any(b => a.IndexOf(b) >= 0)).ToList();
        result.ForEach(a => Console.WriteLine(a));

Sample code...

        List<string> CSS = new List<string>() { "hello", "goodbye" };
        List<string> usedCSS = new List<string>() { "hello darlin", "when are you available", "hello chief", "what", "when to say goodbye" };
        List<string> result = usedCSS.Where(a => CSS.Any(b => a.IndexOf(b) >= 0)).ToList();
        result.ForEach(a => Console.WriteLine(a));
        Console.ReadLine();

result:

hello darlin 
hello chief 
when to say goodbye

or

        List<string> CSS = new List<string>() { "hello", "goodbye" };
        List<string> usedCSS = new List<string>() { "hello darlin", "when are you available", "hello chief", "what" };
        List<string> result = CSS.Where(a => usedCSS.Any(b => b.IndexOf(a) >= 0)).ToList();
        result.ForEach(a => Console.WriteLine(a));
        Console.ReadLine();

result:

hello


Here is more "archived" solution:

List<string> list = css.Where(c => usedCss.Any(c.Contains)).ToList();

You can check, using this test code:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            List<string> css = new List<string>
            {
                ".header {font-size:14px;}",
                ".foo {dfsfd}",
                ".foobar",
                ".wefw"
            };

            List<string> usedCss = new List<string>
            {
                ".header",
                ".foo",
            };

            List<string> list = css.Where(c => usedCss.Any(c.Contains)).ToList();

            if (list.Count > 0)
            {
                Console.WriteLine("Has found in:");
                list.ForEach(Console.WriteLine);
            }
            else
            {
                usedCss.ForEach(x => Console.WriteLine("Could not find: " + x));
            }

            Console.ReadKey();
        }
    }
}

Pay attention, that such code could not distinguish '.foo' from '.foobar'. In general case if it meters you should use more complicated check with regular expressions.

0

精彩评论

暂无评论...
验证码 换一张
取 消