【问题标题】:Check if string contains in array检查字符串是否包含在数组中
【发布时间】:2013-08-16 15:34:59
【问题描述】:

当我在第二个 if 中使用硬编码的“4”时,它可以工作。但我有一个动态字符串 [] ProfileArray 并想检查 View08ListBox1Item 的值是否包含/不包含 ProfileArray 中的字符串之一。为什么它不起作用,当我对 string[] ProfileArray 更改“4”时?

全局:

static string[] ProfileArray;


case "Profile":
            foreach (ListItem View08ListBox1Item in View08ListBox1.Items)
            {
                if (View08ListBox1Item.Selected)
                {
                    if (!View08ListBox1Item.Value.ToString().Contains("4"))
                    {
                        //:do something
                    }
                    else
                    {
                        //:do something
                    }
                }
            }
            break;

这是我的第一个想法,但它不起作用:

case "Profile":
            foreach (ListItem View08ListBox1Item in View08ListBox1.Items)
            {
                if (View08ListBox1Item.Selected)
                {
                    if (!View08ListBox1Item.Value.ToString().Contains(ProfileArray))
                    {
                        //:do something
                    }
                    else
                    {
                        //:do something
                    }
                }
            }
            break;

【问题讨论】:

  • 一个字符串不能包含一个数组。它反过来。
  • 查看Contains 中参数的数据类型:它需要string。字符串数组与普通字符串不同。

标签: c# asp.net arrays string contains


【解决方案1】:

你可以使用 Linq

ProfileArray.Any(x => x == View08ListBoxItem.Value.ToString())  //Contains
!ProfileArray.Any(x => x == View08ListBoxItem.Value.ToString()) //doesn't contain

非 linq 扩展

public static bool Contains<T>(this T[] array, T value) where T : class
{
   foreach(var s in array)
   {
      if(s == value)
      {
         return true;
      }

   }
  return false;
}
ProfileArray.Contains(View08ListBoxItem.Value.ToString());

【讨论】:

  • 或 ProfileArray.All(x=>x == View08ListBoxItem.Value.ToString()) // 不包含
  • @ManuelFischer - 添加了一个可以使用的扩展方法
【解决方案2】:

因为 ProfileArray 是数组而不是字符串。

ProfileArray.Any(x => x == View08ListBox1Item.Value.ToString())

我认为这可行。

在 .NET 2.0 中你可以使用

Array.IndexOf(ProfileArray, View08ListBox1Item.Value.ToString()) == -1

http://msdn.microsoft.com/en-us/library/eha9t187%28v=vs.80%29.aspx

【讨论】:

  • 对于字符串,最好探索 StringComparison 枚举器 x=&gt;x.Equals(View08ListBox1Item.Value.ToString(), StringComparison.*)。更好地控制你想如何匹配,我发现我并不经常关心字符串中的大小写。
  • @kelton52 当然,但这很大程度上取决于信息的类型
  • 是的,只是想扔掉一些信息。
【解决方案3】:

字符串不能包含数组。反之亦然。

你也可以使用非 linq 方式 ProfileArray.Contains(View08ListBox1Item.Value.ToString())

【讨论】:

    【解决方案4】:

    可能是这样的吗?

        bool found = false;
        for ( int i=0; i < ProfileArray.Length; i++)
        {
            if (View08ListBox1.SelectedItem.Value.ToString().Contains(ProfileArray[i])
            {
                found = true;
                break;
            }
        }
    

    如图所示,也不需要迭代列表框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-30
      • 2021-12-14
      • 2021-06-10
      • 1970-01-01
      • 2017-01-18
      • 2013-11-20
      • 2013-11-24
      相关资源
      最近更新 更多