C# 如何检查条件语句中的字符串值列表
我试图检查条件中字符串列表的值,而不创建for-each或counter循环 比如说C# 如何检查条件语句中的字符串值列表,c#,list,C#,List,我试图检查条件中字符串列表的值,而不创建for-each或counter循环 比如说 我想我可以使用一个.contains函数,它需要一个字符串,而不是字符串列表,所以它不会工作。您只需要翻转变量即可。您正在检查扩展名是否包含在ValidExtrade列表中: 你只需要翻转你的变量。您正在检查扩展名是否包含在ValidExtrade列表中: 这个怎么样 using System; using System.Collections.Generic; using System.Linq; using
我想我可以使用一个.contains函数,它需要一个字符串,而不是字符串列表,所以它不会工作。您只需要翻转变量即可。您正在检查扩展名是否包含在ValidExtrade列表中:
你只需要翻转你的变量。您正在检查扩展名是否包含在ValidExtrade列表中: 这个怎么样
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace EasyE
{
class Program
{
static void Main(string[] args)
{
// Test strings
List<extention> extentions = new List<extention>() {
new extention { FileName = "File1.jif" },
new extention() { FileName = "File2.TIFF" },
new extention() { FileName = "File3.txt" } };
///////////////
Regex regex = new Regex(@"^.*\.(tif|tiff|gif|jpeg|jif)$",RegexOptions.IgnoreCase);
if ((from ext in extentions where (ext.FileName.ToString() == regex.Match(ext.FileName.ToString()).Value) select ext).Count() > 0)
{
}
}
}
class extention
{
public string FileName { get; set; }
}
}
这个怎么样
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace EasyE
{
class Program
{
static void Main(string[] args)
{
// Test strings
List<extention> extentions = new List<extention>() {
new extention { FileName = "File1.jif" },
new extention() { FileName = "File2.TIFF" },
new extention() { FileName = "File3.txt" } };
///////////////
Regex regex = new Regex(@"^.*\.(tif|tiff|gif|jpeg|jif)$",RegexOptions.IgnoreCase);
if ((from ext in extentions where (ext.FileName.ToString() == regex.Match(ext.FileName.ToString()).Value) select ext).Count() > 0)
{
}
}
}
class extention
{
public string FileName { get; set; }
}
}
你应该考虑可变套管吗?可能是ValidExtensions。ContainesTention,StringComparer。CurrentCultureInogoreCase?@roryap-可能,但OP没有提到他们需要担心这个案子。如果他们这样做了,那么使用包含重载就很简单了@EasyE-对于小示例来说这很好,但是鉴于字符串是不可变的,不建议使用这种类型的字符串操作。我建议在比较字符串时使用大小写不变的选项,比如我的第一条注释中的重载,忘记转换成小写。只是未来的注意事项。你应该考虑可变套管吗?可能是ValidExtensions。ContainesTention,StringComparer。CurrentCultureInogoreCase?@roryap-可能,但OP没有提到他们需要担心这个案子。如果他们这样做了,那么使用包含重载就很简单了@EasyE-对于小示例来说这很好,但是鉴于字符串是不可变的,不建议使用这种类型的字符串操作。我建议在比较字符串时使用大小写不变的选项,比如我的第一条注释中的重载,忘记转换成小写。只是给未来的一个提示。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace EasyE
{
class Program
{
static void Main(string[] args)
{
// Test strings
List<extention> extentions = new List<extention>() {
new extention { FileName = "File1.jif" },
new extention() { FileName = "File2.TIFF" },
new extention() { FileName = "File3.txt" } };
///////////////
Regex regex = new Regex(@"^.*\.(tif|tiff|gif|jpeg|jif)$",RegexOptions.IgnoreCase);
if ((from ext in extentions where (ext.FileName.ToString() == regex.Match(ext.FileName.ToString()).Value) select ext).Count() > 0)
{
}
}
}
class extention
{
public string FileName { get; set; }
}
}