我有这样一个名单:
item.Add("a");
item.Add("as");
item.Add("b");
item.Add("fgs");
item.Add("adsd");
我如何找到从(例如)“a”开始的所有项目?
这种“a”并不是一些硬编码,因此,我需要一种功能,为每一条扼杀都这样做。
我与FindAll进行了尝试,但我没有说明它是如何运作的。
Br, Wolfy
我有这样一个名单:
item.Add("a");
item.Add("as");
item.Add("b");
item.Add("fgs");
item.Add("adsd");
我如何找到从(例如)“a”开始的所有项目?
这种“a”并不是一些硬编码,因此,我需要一种功能,为每一条扼杀都这样做。
我与FindAll进行了尝试,但我没有说明它是如何运作的。
Br, Wolfy
如果用“起步”指第一个char果,那么:
item.FindAll(i => i[0] == a );
如果你指体(可能小于1焦炭),那么:
item.FindAll(i => i.StartsWith("a"));
如果你们想要进行不同的比较,例如对个案不敏感、基于当地等。 之后是相关指数Of,例如:
item.FindAll(i => i.IndexOf("a", StringComparison.CurrentCultureIgnoreCase) == 0);
以上所有内容都可轻易加以调整,以使用相关的果园或铺设变量或参数。
如果你不需要清单提供的额外财产和方法,那么使用<代码>的效率就会提高。 在<代码>以下编码>以<代码>.Find All为代码>的上设定新的名单,并一度填入。 在
将列出对应结果,因为其通过该表而重新编号。
Or with LINQ
from i in items where i.StartsWith("a") select i;
for NET2.0 you may use this method: pattern is an argument to look for (f.e. "a")
private List<string> FindAll(List<string> list, string pattern)
{ // returns found results
return list.FindAll(delegate(string item)
{
return item.StartsWith(pattern);
});
}
我认为,还有一份清单,载有标准说明的起点。 让我们把“词”和其他清单“关键词”称作“关键词”。 因此,下面的询问将回去你想要的东西。
List<string> words = new List<string>();
words.Add("a");
words.Add("as");
words.Add("b");
words.Add("fgs");
words.Add("adsd");
List<string> keywords = new List<string>();
keywords.Add("a");
keywords.Add("b");
var result = words.FindAll(o =>
keywords.Any(a => o.StartsWith(a))
);
其结果是,从关键词开始使用任何关键词。
List<string> item = new List<string>();
item.Add("a");
item.Add("as");
item.Add("b");
item.Add("fgs");
item.Add("adsd");
var res1 = item.FindAll(i => i.StartsWith("a"));
var res2 = item.Where(i => i.StartsWith("a"));
引言
item.FindAll(i => i.Contains("a"));
这份清单只载有过滤器。
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...