c#包含部分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9923158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:14:25  来源:igfitidea点击:

c# contains part of string

c#stringcontains

提问by raze

So I have a list with Materiel-objects. In Materiel I have 15 get and set methods. I want to construct a search-method that loops all the objects in the list, and all of the variables in each Materiel-object. The looping part is easy enough, but I'm struggling with the string-contains-part. The search term could for instance be "acto", and I should get a hit for "Tractor". I have tried using the string-Contains class, but as far as I can figure out, it only checks the string beginning in position 0. So "Tra" gets a hit, but not "acto".

所以我有一个带有Materiel-objects的列表。在 Materiel 中,我有 15 个 get 和 set 方法。我想构建一个搜索方法,循环列表中的所有对象,以及每个 Materiel 对象中的所有变量。循环部分很容易,但我正在努力处理字符串包含部分。例如,搜索词可能是“acto”,我应该会搜索到“拖拉机”。我曾尝试使用 string-Contains 类,但据我所知,它只检查从位置 0 开始的字符串。所以“Tra”得到了命中,但“acto”没有得到命中。

Is there any build in classes, or should I program one myself?

是否有任何内置课程,还是我应该自己编程?

Sorry for the bad explanation.

对不起,不好的解释。

My code. I see now that I get hits for the substring, but also other results :)

我的代码。我现在看到我得到了子字符串的点击,还有其他结果:)

    protected void Button_search_Click(object sender, EventArgs e)
    {
        string searchTerm = TextBox1.Text.ToString().ToLower();

        TableRow row;
        TableCell cell;

        int rowNumber = 1;

        foreach (Materiell mat in allItems)
        {
            if (searchTerm.Contains(mat.itemID.ToString().ToLower()) ||
                searchTerm.Contains(mat.manufacturer.ToLower()) ||
                searchTerm.Contains(mat.model.ToLower()) ||
                searchTerm.Contains(mat.serialNo.ToLower()) ||
                searchTerm.Contains(mat.dateProd.ToString().ToLower()) ||
                searchTerm.Contains(mat.location.ToLower()) ||
                searchTerm.Contains(mat.mainCategory.ToLower()) ||
                searchTerm.Contains(mat.subCategory.ToLower()) ||
                searchTerm.Contains(mat.dateAcquired.ToString().ToLower()) ||
                searchTerm.Contains(mat.price.ToString().ToLower()) ||
                searchTerm.Contains(mat.ownerID.ToString().ToLower()) ||
                searchTerm.Contains(mat.extra.ToString().ToLower()) ||
                searchTerm.Contains(mat.textComment.ToLower()) ||
                searchTerm.Contains(mat.active.ToString().ToLower()))
            {
                row = new TableRow();
                row.ID = "row" + rowNumber.ToString();
                rowNumber++;

                cell = new TableCell();
                cell.Text = "<a href=\"#\" class=\"opendiv\">" + mat.itemID.ToString() + "</a>";
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.manufacturer.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.model.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.serialNo.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.dateProd.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.location.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.mainCategory.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.subCategory.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.dateAcquired.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.price.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.ownerID.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.extra.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.ownDefData.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.textComment.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = mat.active.ToString();
                row.Cells.Add(cell);

                Table1.Rows.Add(row);
            }
        }
    }

采纳答案by ilivewithian

"some string".Contains("str")will return true, are you having problems with case sesitivity?

"some string".Contains("str")将返回 true,您是否有区分大小写的问题?

If so you could use this:

如果是这样,你可以使用这个:

public static bool Contains(this string source, string toCheck, StringComparison comp) {
  return source.IndexOf(toCheck, comp) >= 0;
}

string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);

(Taken from Case insensitive 'Contains(string)')

(取自不区分大小写的 'Contains(string)'

回答by SkonJeet

 Bool doesContain = "Tractor".Contains("acto");

doesContain will be true.

doContain 将是真的。

Contains works on collections, and in this instance I thinkthe string is treated as a collection of chars.

包含对集合的工作,在这种情况下,我认为字符串被视为字符集合。

http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx

http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx

回答by Doomsknight

Use IndexOf

IndexOf

string searchWithinThis = "ABCDEFGHIJKLMNOP";
string searchForThis = "DEF";
int firstCharacter = searchWithinThis.IndexOf(searchForThis);

Console.WriteLine("First occurrence: {0}", firstCharacter);

if substring is not found, it returns -1. Its very useful for also knowing where abouts the string is found.

如果未找到子字符串,则返回 -1。它对于了解字符串的位置非常有用。

回答by Guffa

The string.Containsmethod does look for the substring anywhere in the string.

string.Contains方法会在字符串中的任何位置查找子字符串。

"asdf".Contains("as") --> True
"asdf".Contains("sd") --> True
"asdf".Contains("df") --> True
"asdf".Contains("xy") --> False

The comparison is however case sensetive, so you might need to convert case if you want to make an case insesetive search:

然而,比较是区分大小写的,因此如果您想进行不区分大小写的搜索,您可能需要转换大小写:

"Asdf".Contains("as") --> False
"Asdf".Contains("As") --> True

"Asdf".ToUpper().Contains("as".ToUpper()) --> True

回答by Sunil Chavan

class SearchInString
{
    static void Main()
    {
        string strn= "A great things are happen with great humans.";
        System.Console.WriteLine("'{0}'",strn);

        bool case1= strn.StartsWith("A great");
        System.Console.WriteLine("starts with 'A great'? {0}", case1);

        bool case2= strn.StartsWith("A great", System.StringComparison.OrdinalIgnoreCase);
        System.Console.WriteLine("starts with 'A great'? {0} (ignoring case)", case2);

        bool case3= strn.EndsWith(".");
        System.Console.WriteLine("ends with '.'? {0}", case3);

        int start= strn.IndexOf("great");
        int end= strn.LastIndexOf("great");
        string strn2 = strn.Substring(start, end- start);
        System.Console.WriteLine("between two 'great' words: '{0}'", strn2);
    }
}

回答by sehe

For shits and giggles I thought it was a nice lunch-break project to come up with a simple, but 'elegant' solution to the Question (as I understood it :)):

对于狗屎和傻笑,我认为提出一个简单但“优雅”的问题解决方案是一个不错的午休项目(据我了解:)):

E.g.

例如

// I made up a Material class for testing:
public class Materiel
{
    public string A { get; set; }
    public int B { get; set; }
    public DateTime? C { get; set; }
    public string D { get; set; }
    public Nested E { get; set; }
}     

// [...] usage:

foreach (var pattern in new[]{ "World" , "dick", "Dick", "ick", "2012", "Attach" })
    Console.WriteLine("{0} records match '{1}'", Database.Search(pattern).Count(), pattern);

Outputs:

输出:

2 records match 'World'
1 records match 'dick'
1 records match 'Dick'
2 records match 'ick'
1 records match '2012'
2 records match 'Attach'

The code also supports

代码还支持

  • regex matches
  • any property types (e.g. nullable DateTimes or nested classes)
  • showing whichproperty matched the pattern/substring
  • 正则匹配
  • 任何属性类型(例如可为空的 DateTime 或嵌套类)
  • 显示哪个属性与模式/子字符串匹配

Enjoy:

享受:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

namespace AClient
{
    public class Materiel
    {
        public string A { get; set; }
        public int B { get; set; }
        public DateTime? C { get; set; }
        public string D { get; set; }
        public Nested E { get; set; }
    }

    public struct Nested
    {
        public string Data { get; set; }
        public override string ToString() { return string.Format("Extra: {0}", Data); }
    }


    public static class FullText
    {
        public class PropMatched<T> { public PropertyInfo Property; public T Item; }

        public static IEnumerable<PropMatched<T> > ByProperty<T>(this IEnumerable<T> items, string substr)
        {
            return items.ByProperty(new Regex(Regex.Escape(substr), RegexOptions.IgnoreCase));
        }

        public static IEnumerable<PropMatched<T> > ByProperty<T>(this IEnumerable<T> items, Regex pattern)
        {
            return items.Select(i => i.MatchingProperties(pattern)).Where(m => null != m);
        }

        public static IEnumerable<T> Search<T>(this IEnumerable<T> items, string substr)
        {
            return items.Search(new Regex(Regex.Escape(substr), RegexOptions.IgnoreCase));
        }

        public static IEnumerable<T> Search<T>(this IEnumerable<T> items, Regex pattern)
        {
            return items.Where(i => null != i.MatchingProperties(pattern));
        }

        public static PropMatched<T> MatchingProperties<T>(this T item, Regex pattern)
        {
            if (null == pattern || null == item) return null;

            var properties = item.GetType().GetProperties(BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance);
            var matches = from   prop in properties 
                          let    val = prop.GetGetMethod(true).Invoke(item, new object[]{}) 
                          where  pattern.IsMatch((val??"").ToString()) 
                          select prop;

            var found = matches.FirstOrDefault();
            return found == null ? null : new PropMatched<T> {Item = item, Property = found};
        }
    }

    class Client
    {
        private static readonly IEnumerable<Materiel> Database = new List<Materiel>
            {
                new Materiel {
                        A = "Hello", B = 1, C = null, D = "World",
                        E = new Nested {Data = "Attachment"}
                    },
                new Materiel {
                        A = "Transfigured", B = 2, C = null, D = "Nights",
                        E = new Nested {Data = "Schoenberg"}
                    },
                new Materiel {
                        A = "Moby", B = 3, C = null, D = "Dick",
                        E = new Nested {Data = "Biographic"}
                    },
                new Materiel {
                        A = "Prick", B = 4, C = DateTime.Today, D = "World",
                        E = new Nested {Data = "Attachment"}
                    },
                new Materiel {
                        A = "Oh Noes", B = 2, C = null, D = "Nights",
                        E = new Nested {Data = "Schoenberg"}
                    },
            };


        static void Main()
        {
            foreach (var pattern in new[]{ "World" , "dick", "Dick", "ick", "2012", "Attach" })
                Console.WriteLine("{0} records match '{1}'", Database.Search(pattern).Count(), pattern);

            // regex sample:
            var regex = new Regex(@"N\w+s", RegexOptions.IgnoreCase);

            Console.WriteLine(@"{0} records match regular expression 'N\w+s'", Database.Search(regex).Count());

            // with context info:
            foreach (var contextMatch in Database.ByProperty(regex))
            {
                Console.WriteLine("1 match of regex in propery {0} with value '{1}'",
                    contextMatch.Property.Name, contextMatch.Property.GetGetMethod().Invoke(contextMatch.Item, new object[0]));

            }
        }
    }
}