字符串数组。包含?

发布于 2024-09-27 03:44:10 字数 167 浏览 1 评论 0 原文

.NET 2

string[] myStrings = GetMyStrings();    
string test = "testValue";

如何验证 myStrings 是否包含 test

.NET 2

string[] myStrings = GetMyStrings();    
string test = "testValue";

How can I verify if myStrings contains test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

天荒地未老 2024-10-04 03:44:10

在 .NET 2.0 中,如果需要索引,可以执行以下操作:

int index = Array.FindIndex(
    myStrings,
    delegate(string s) { return s.Equals(test); }
);

如果 myStrings 不包含 ,则 index 将为 -1测试。

如果您只想检查是否存在:

bool exists = Array.Exists(
    myStrings,
    delegate(string s) { return s.Equals(test); }
);

In .NET 2.0, you could do the following if you want the index:

int index = Array.FindIndex(
    myStrings,
    delegate(string s) { return s.Equals(test); }
);

index will be -1 if myStrings does not contain test.

If you merely want to check for existence:

bool exists = Array.Exists(
    myStrings,
    delegate(string s) { return s.Equals(test); }
);
无人问我粥可暖 2024-10-04 03:44:10

我在这里的页面找到了一个优雅的答案 http:// www.dotnettoad.com/index.php?/archives/10-Array.Contains.html。在 .NET 2.0 中您要做的就是转换为 IList 并调用 Contains 方法。

(IList<string> mystrings).Contains(test);

I have found an elegant answer at the page here http://www.dotnettoad.com/index.php?/archives/10-Array.Contains.html. What you have to do in .NET 2.0 is to cast to IList and call Contains method.

(IList<string> mystrings).Contains(test);
一个人的夜不怕黑 2024-10-04 03:44:10

这是一种符合 .NET 2.0 的方法。如果未找到该值,则使用 Array.Find 将返回 null。

C# 方法

string[] myStrings = { "A", "B", "testValue" };
string test = "testValue";
string result = Array.Find(myStrings, delegate(string s) { return s == test; });
Console.WriteLine("Result: " + result);

如果您需要不区分大小写的匹配,请使用s.Equals(test, StringComparison.InvariantCultureIgnoreCase)

编辑:使用 VB.NET 2.0 需要付出更多努力,因为它不支持匿名委托。相反,您需要添加一个 Function 并使用 AddressOf 指向它。您需要将 testValue 设置为成员或属性,因为它不会传递到谓词方法。在下面的示例中,我使用 Array.Exists。

VB.NET 方法

' field or property ' 
Dim test As String = "testValue"

Sub Main
    Dim myStrings As String() = { "A", "B", "testValue" }       
    Dim result As Boolean = Array.Exists(myStrings, AddressOf ContainsValue)
    Console.WriteLine(result)
End Sub

' Predicate method '
Private Function ContainsValue(s As String) As Boolean
    Return s = test
End Function

Here's a .NET 2.0 compliant approach. Using Array.Find will return null if the value isn't found.

C# Approach

string[] myStrings = { "A", "B", "testValue" };
string test = "testValue";
string result = Array.Find(myStrings, delegate(string s) { return s == test; });
Console.WriteLine("Result: " + result);

If you need a case insensitive match use s.Equals(test, StringComparison.InvariantCultureIgnoreCase).

EDIT: with VB.NET 2.0 more effort is required since it doesn't support anonymous delegates. Instead you would need to add a Function and use AddressOf to point to it. You would need to set the testValue as a member or property since it will not be passed in to the predicate method. In the following example I use Array.Exists.

VB.NET Approach

' field or property ' 
Dim test As String = "testValue"

Sub Main
    Dim myStrings As String() = { "A", "B", "testValue" }       
    Dim result As Boolean = Array.Exists(myStrings, AddressOf ContainsValue)
    Console.WriteLine(result)
End Sub

' Predicate method '
Private Function ContainsValue(s As String) As Boolean
    Return s = test
End Function
仙气飘飘 2024-10-04 03:44:10

您可以使用列表而不是使用静态数组:

List<string> myStrings = new List<string>(GetMyStrings());
if(myStrings.Contains("testValue"))
{
    // Do Work
}

Instead of using a static array, you could use a List:

List<string> myStrings = new List<string>(GetMyStrings());
if(myStrings.Contains("testValue"))
{
    // Do Work
}
不爱素颜 2024-10-04 03:44:10

我想我会添加另一个,首先在 .NET 3.5 中提供,我相信:

Enumerable.Contains(myStrings.ToArray(), test)

Thought I would add another to the mix, first available in .NET 3.5, I believe:

Enumerable.Contains(myStrings.ToArray(), test)
浮华 2024-10-04 03:44:10
bool ContainsString(string[] arr, string testval)
{
    if ( arr == null )
        return false;
    for ( int i = arr.Length-1; i >= 0; i-- )
        if ( arr[i] == testval )
            return true;
    return false;
}

这将是有史以来最好的表现。 :P

bool ContainsString(string[] arr, string testval)
{
    if ( arr == null )
        return false;
    for ( int i = arr.Length-1; i >= 0; i-- )
        if ( arr[i] == testval )
            return true;
    return false;
}

And this will have the best performance ever. :P

笑咖 2024-10-04 03:44:10

msdn 上有几乎完全相同的问题。
在字符串数组中查找字符串
正如其他人所说,你确实有两个选择:
1)使用列表更容易检查
2)循环遍历数组以查找字符串

Here is almost the exact same question on msdn.
Find String in String Array
As others have said you really have two options:
1) Use a list for easier checking
2) Loop through your array to find the string

花开浅夏 2024-10-04 03:44:10

您可以按如下所述使用 Array.BinarySearch。

 string[] strArray = GetStringArray();
        string strToSearch ="test";
        Array.BinarySearch(strArray, strToSearch);

you can use Array.BinarySearch as described below.

 string[] strArray = GetStringArray();
        string strToSearch ="test";
        Array.BinarySearch(strArray, strToSearch);
萌逼全场 2024-10-04 03:44:10

怎么样:

Sub Main
    Dim myStrings(4) As String
    myStrings(0) = "String 1"
    myStrings(1) = "String 2"
    myStrings(2) = "testValue"
    myStrings(3) = "String 3"
    myStrings(4) = "String 4"

    Dim test As String = "testValue"

    Dim isFound As Boolean = Array.IndexOf(myStrings, test) >= 0

    If isFound Then
        Console.WriteLine("Found it!")
    End If
End Sub

这应该适用于.Net 2.0 和 VB.Net。

How about this:

Sub Main
    Dim myStrings(4) As String
    myStrings(0) = "String 1"
    myStrings(1) = "String 2"
    myStrings(2) = "testValue"
    myStrings(3) = "String 3"
    myStrings(4) = "String 4"

    Dim test As String = "testValue"

    Dim isFound As Boolean = Array.IndexOf(myStrings, test) >= 0

    If isFound Then
        Console.WriteLine("Found it!")
    End If
End Sub

This should work for .Net 2.0 and VB.Net.

颜漓半夏 2024-10-04 03:44:10

我假设您想检查数组中的任何元素是否包含特定值(测试)。如果是这样,您想构造一个简单的循环。事实上,我认为你应该 点击此处

I assume you want to check if any elements in your array contains a certain value (test). If so you want to construct a simple loop. In fact I think you should click here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文