匹配另一个List <string> </string> </string>中List <string>中的部分字符串

时间:2011-07-19 21:54:16

标签: c# string list match

它是我的。我得到了以下代码:

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>

但是,我需要反过来......

我希望var str in usedCSSvar str in CSS

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

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

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

4 个答案:

答案 0 :(得分:3)

如果我理解你正在尝试做什么,你可以做到这一点。 FirstOrDefault()找到第一次出现的谓词匹配并返回它,如果找不到,则null(类型为string的默认值)。然后,我们需要的是StartsWith()字符串方法,以便在前缀方面将每个项目与所讨论的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 />");
            }
        }

P.S。如果你只想要一个匹配或没有匹配,如果你可以有多个匹配,并且想要它们全部,则上述方法有效:

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

答案 1 :(得分:1)

尝试

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

答案 2 :(得分:0)

        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

答案 3 :(得分:0)

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

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'区分开来。一般情况下,如果它计量,你应该使用正则表达式进行更复杂的检查。