匹配 List中的部分字符串在另一个列表

发布于 2024-11-25 06:51:00 字数 707 浏览 2 评论 0原文

这就是我。我得到以下代码:

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

CSS = List

但是,我需要反之亦然...

我希望 var str inusedCSSvar str in CSS

usedCSS 仅包含 css 名称的字符串例如:.header

CSS 包含实际 css 的字符串,例如: .header {font-size:14px;}

基本上,我需要的是打印出使用的实际 CSS。 我目前的代码恰恰相反,它只返回 css 名称, 不是实际的CSS。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明明#如月 2024-12-02 06:51:00

如果我明白你想要正确做什么,你就可以这样做。 FirstOrDefault() 查找谓词匹配的第一个匹配项并返回它,如果未找到,则返回 nullstring 类型的默认值)。然后我们需要的只是 StartsWith() 字符串方法,将 CSS 中的每个项目按前缀与相关的 str 进行匹配。

        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 />");
            }
        }

ps 如果您只期望一个匹配或没有匹配,如果您可以有多个匹配,并且想要全部匹配,则上述方法有效:

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

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 />");
    }
}
迷途知返 2024-12-02 06:51:00

尝试

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 />");
} 

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 />");
} 
苍暮颜 2024-12-02 06:51:00
        List<string> result = CSS.Where(a => usedCSS.Any(b => b.IndexOf(a) >= 0)).ToList();
        result.ForEach(a => Console.WriteLine(a));

以及相反的

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

示例代码...

        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();

结果:

hello darlin 
hello chief 
when to say goodbye

        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();

结果:

hello
        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
做个少女永远怀春 2024-12-02 06:51:00

这是更多“存档”的解决方案:

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

您可以使用以下测试代码进行检查:

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();
        }
    }
}

请注意,此类代码无法区分 '.foo''.foobar'。一般情况下,如果它计量,您应该使用正则表达式进行更复杂的检查。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文