如何检查文件名是否包含整个文件名中的某些字符串值
How to check whether the file name contains some strings values in entire file name
I have a File names of around 37k and have 8k of string array. i want to check each individual string in an array is present in those File name and it should return that file name. I have tried below code. But not getting the required answer.
foreach (string result in DifferArray)
{
var res = result ;
var results = files.Contains(res);
if (results == true)
{
}
else
{
}
}
}
我附上了下面的文件名图片
for example if res has a value of '000000000000000100108979' and filename contain this then it should return the file name.
一个快速的解决方案可能是:
var results = files.Any(r => r.IndexOf(res) >= 0);
那是因为 Contains
搜索匹配的字符串,而不仅仅是它的一部分。
可以优化算法并尝试其他方法,但这只是一个简单的修复。
根据操作请求进行编辑
要获取文件名,您可以使用:
var filename = files.FirstOrDefault(r => r.IndexOf(res) >= 0);
if (string.IsNullOrEmpty(filename)) {
// NOT FOUND
} else {
// Filename contains your full filename
}
I have a File names of around 37k and have 8k of string array. i want to check each individual string in an array is present in those File name and it should return that file name. I have tried below code. But not getting the required answer.
foreach (string result in DifferArray)
{
var res = result ;
var results = files.Contains(res);
if (results == true)
{
}
else
{
}
}
}
我附上了下面的文件名图片
for example if res has a value of '000000000000000100108979' and filename contain this then it should return the file name.
一个快速的解决方案可能是:
var results = files.Any(r => r.IndexOf(res) >= 0);
那是因为 Contains
搜索匹配的字符串,而不仅仅是它的一部分。
可以优化算法并尝试其他方法,但这只是一个简单的修复。
根据操作请求进行编辑
要获取文件名,您可以使用:
var filename = files.FirstOrDefault(r => r.IndexOf(res) >= 0);
if (string.IsNullOrEmpty(filename)) {
// NOT FOUND
} else {
// Filename contains your full filename
}