简体   繁体   English

我们如何在不使用循环的情况下匹配包含 C# 中数组的任何值的字符串?

[英]How can we match string contains any value of array in C# w/o using loop?

How can we match string contains any value of array in C# w/o using loop?我们如何在不使用循环的情况下匹配包含 C# 中数组的任何值的字符串?

For example:例如:

string[] abc= [val1,val2,val3] . string[] abc= [val1,val2,val3] 。 string xyz= "demo string to check if it contains any value in abc array that is val2"; string xyz="演示字符串,检查它是否包含 abc 数组中的任何值,即 val2";

I want to check if any value of array abc exists in string xyz w/o using loop我想使用循环检查字符串 xyz 中是否存在数组 abc 的任何值 w/o

one aproach may be linq一种方法可能是 linq

string[] abc = { "abc", "def", "xyz" }; 
string xyz = "demo string to check if it contains any value in abc array that is val2";
bool result = abc.Any(xyz.Contains); //true

but internal linq and contains uses loop - so i think there is no possible solution without a loop.但是内部 linq 和 contains 使用循环 - 所以我认为没有循环就没有可能的解决方案。

To search from/in a collection (read array), you need to use loops/iteration because collection is involved.要从/在集合中搜索(读取数组),您需要使用循环/迭代,因为涉及到集合。

Even if you do not write a loop and write some syntactic sugar code, in the background it will use LOOP/ITERATION.即使你不写循环,写一些语法糖代码,在后台它也会使用 LOOP/ITERATION。

As others answered you can use LINQ or something but it will iterate over your collection.正如其他人回答的那样,您可以使用 LINQ 或其他东西,但它会遍历您的集合。

There is already a contains function.已经有一个包含功能。 you may use it like this你可以这样使用它

bool result=abc.Contains("walue to be checked");

Alternate way is by using LINQ替代方法是使用 LINQ

but both the method use internal loops.但是这两种方法都使用内部循环。

you can check as :您可以检查为:

string[] arr = { "abc", "bcd", "cde" };

if (arr.Contains("abc"))
{
    Console.WriteLine("Found");
}
else
{
    Console.WriteLine("Not Found");
}

For faster way you can use Array.IndexOf Method :对于更快的方法,您可以使用Array.IndexOf 方法

int pos = Array.IndexOf(arr, "abc");
if (pos > -1)
{
    // the array contains the string and the pos variable
    // will have its position in the array
}

暂无
暂无

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

相关问题 C#/。NET,如何使用纯字符串操作以编程方式加载,编辑和保存w / o源代码文件? - C#/.NET, How can I programmatically load, edit, and save source code files w/o using plain string manipulation? 如何检查C#字符串数组是否包含值 - How can I check to see if a C# string array contains a value C#-使用字符串包含与字符串数组 - C# - using string contains with string array 如何检查字符串数组中的任何字符串是否包含在 c# 中的另一个字符串中 - How to check if any string from array of string contains in another string in c# C# 检查字符串是否包含字符串数组中的任何匹配项 - C# Check if string contains any matches in a string array 在C#中使用string.Contains()方法匹配精确的字符串 - Match exact string using string.Contains() method in c# 暂停while循环,直到使用事件处理程序C#完全按下按钮为止 - Pause the while loop until the button is pressed w/o using event handler C# 使用 C# 检查字符串是否包含字符串数组中的字符串 - Using C# to check if string contains a string in string array XML C# 如果多个子元素中的任何一个包含数组中的值,如何删除父元素 - XML C# How to remove parent element if any of multiple child elements contains a value in an array c# - 可以通过值或作为 object 传递参数到 function 无过载 - c# - can one pass parameters by value or as an object to function w/o overloading it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM