简体   繁体   English

获取字符串中包含的实例列表中的计数

[英]Get Count in List of instances contained in a string

I have a string containing up to 9 unique numbers from 1 to 9 (myString) eg "12345"我有一个字符串,最多包含 9 个从 1 到 9的唯一数字(myString),例如“12345”

I have a list of strings {"1"}, {"4"} (myList).. and so on.我有一个字符串列表 {"1"}、{"4"} (myList).. 等等。

I would like to know how many instances in the string (myString) are contained within the list (myList), in the above example this would return 2.我想知道字符串(myString)中有多少个实例包含在列表(myList)中,在上面的示例中,这将返回 2。

so something like所以像

count = myList.Count(myList.Contains(myString));

I could change myString to a list if required.如果需要,我可以将 myString 更改为列表。

Thanks very much非常感谢

Joe

I would try the following:我会尝试以下方法:

count = mylist.Count(s => myString.Contains(s));

It is not perfectly clear what you need, but these are some options that could help:目前尚不清楚您需要什么,但这些选项可能会有所帮助:

myList.Where(s => s == myString).Count()

or或者

myList.Where(s => s.Contains(myString)).Count()

the first would return the number of strings in the list that are the same as yours, the second would return the number of strings that contain yours.第一个将返回列表中与您的相同的字符串数,第二个将返回包含您的字符串的数。 If neither works, please make your question more clear.如果两者都不起作用,请让您的问题更清楚。

If myList is just List<string> , then this should work:如果myList只是List<string> ,那么这应该有效:

int count = myList.Count(x => myString.Contains(x));

If myList is List<List<string>> :如果myListList<List<string>>

int count = myList.SelectMany(x => x).Count(s => myString.Contains(s));

This is one approach, but it's limited to 1 character matches.这是一种方法,但仅限于 1 个字符匹配。 For your described scenario of numbers from 1-9 this works fine.对于您描述的 1-9 数字场景,这很好用。 Notice the s[0] usage which refers to the list items as a character.请注意s[0]用法,它将列表项称为字符。 For example, if you had "12" in your list, it wouldn't work correctly.例如,如果您的列表中有“12”,它将无法正常工作。

string input = "123456123";
var list = new List<string> { "1", "4" };
var query = list.Select(s => new
                {
                    Value = s,
                    Count = input.Count(c => c == s[0])
                });

foreach (var item in query)
{
    Console.WriteLine("{0} occurred {1} time(s)", item.Value, item.Count);
}

For multiple character matches, which would correctly count the occurrences of "12", the Regex class comes in handy:对于多个字符匹配,可以正确计算“12”的出现,正则Regex class 派上用场:

var query = list.Select(s => new
                {
                    Value = s,
                    Count = Regex.Matches(input, s).Count
                });

Try尝试

count = myList.Count(s => s==myString);

try尝试

var count = myList.Count(x => myString.ToCharArray().Contains(x[0]));

this will only work if the item in myList is a single digit这仅在 myList 中的项目是单个数字时才有效

Edit: as you probably noticed this will convert myString to a char array multiple times so it would be better to have编辑:正如您可能注意到的那样,这会将 myString 多次转换为 char 数组,所以最好有

var myStringArray = myString.ToCharArray();
var count = myList.Count(x => myStringArray.Contains(x[0]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM