简体   繁体   English

如何检查 String 是否以列表中的内容结尾。 C#

[英]How to check if String ends with something from a list. C#

I want to take a user's input, and check if the end of what they put in ends with something.我想接受用户的输入,并检查他们输入的内容是否以某些内容结束。 But it's more than one string.但它不止一个字符串。 I have it in a list.我有它在一个列表中。 And I could check if the input ends with a string from the list one by one.我可以检查输入是否以列表中的字符串结尾。 However, I would just like to check if the input ends with anything from a list.但是,我只想检查输入是否以列表中的任何内容结尾。

If "endings" is a List<string> that contains the possible endings to match:如果 "endings" 是一个 List<string> ,其中包含要匹配的可能结尾:

if (endings.Any(x => userInput.EndsWith(x)))
{
    // the string ends with something in the list
}
string[] imageEndsWith = { ".jpeg", ".JPEG", ".png", ".PNG", ".jpg", ".JPG" };
if (imageEndsWith.Any(x => _fileName.EndsWith(x))) {
    //your code goes here
}
else {
}

what this code does is create an array of string(imageEndsWith) any of which you want to find at the end of another string(_fileName).这段代码的作用是创建一个字符串数组(imageEndsWith),您希望在另一个字符串(_fileName)的末尾找到其中的任何一个。

For example This code will find the jpeg or jpg or png images when the name of file is in the variable _fileName例如,当文件名在变量 _fileName 中时,此代码将查找 jpeg 或 jpg 或 png 图像

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

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