我正在尝试找到一种最简单的方法来搜索string
可能的string
数组。我知道为角色执行此操作的简单方法是使用myString.IndexOfAny(charArray)
。但是,如果我想在string
搜索string
而不仅仅是字符,该怎么办?是否有任何.net技巧或方法使这更容易?
基本上,我想做这样的事情:
string myName = "rahkim";
string[] names = new string[] {"joe","bob","chris"};
if(myName.IndexOfAny(names) >= 0)
{
//success code//
}
我知道有很多方法可以通过循环等来实现这一点。但是我希望框架中固有的东西。
答案 0 :(得分:10)
您应该定义是否要查找相等的字符串或搜索匹配的子字符串。这两种方式都很容易在LINQ之前和LINQ。
string myName = "rahkim";
string[] names = new string[] { "joe", "bob", "chris" };
LINQ等同字符串
bool contains = names.Contains(myName);
LINAL之前的平等字符串
bool contains = new List<string>(name).Contains(myName);
子串,LINQ
bool contains = names.Any(name => name.Contains(myName));
子串,Pre-LINQ
bool contains = false;
foreach(string name in names)
if (name.Contains(myName))
contains = true;
答案 1 :(得分:8)
如果其他人在尝试搜索像String.IndexOfAny(String [])这样的.Net方法时发现了这个,这是我的解决方案:
<强> C#强>
public int IndexOfAny(string test, string[] values)
{
int first = -1;
foreach (string item in values) {
int i = test.IndexOf(item);
if (i >= 0) {
if (first > 0) {
if (i < first) {
first = i;
}
} else {
first = i;
}
}
}
return first;
}
<强> VB 强>
Public Function IndexOfAny(test As String, values As String()) As Integer
Dim first As Integer = -1
For Each item As String In values
Dim i As Integer = test.IndexOf(item)
If i >= 0 Then
If first > 0 Then
If i < first Then
first = i
End If
Else
first = i
End If
End If
Next
Return first
End Function
只需切换
即可执行LastIndexOfAny(String [])i < first
到
i > first
答案 2 :(得分:5)
您也可以(也)使用static
类的IndexOf
Array
方法:
bool hasName = Array.IndexOf(names, myName) > -1;
答案 3 :(得分:3)
int IndexOfAny(String [] rgs)确实很好,但它名义上是一个O(n ^ 2)操作。如果在您的应用程序中,字符串集 rgs 很大且始终相同,那么最有效的方法是将它们加载到trie数据结构中一次,然后重复使用trie在运行时给出的未知字符串中搜索它们。
以下是相关代码,改编自我在网上找到的C#特里来源,归功于“Kerry D. Wong”。在我的版本中,trie中的每个字符串都具有泛型类型 TValue 的“有效负载”。要使用此trie来简单搜索子字符串,有效负载总是可以设置为 true ,如 simple_trie 所示。
我在这里改变的另一件事是这个trie自动适应允许存储任意Unicode字符串。每个节点上的数组 - 表征特里 - 调整其基数和长度,以适应需要存储在该节点的Unicode字符范围。例如,这允许区分大小写的匹配。
C#3.0初始化语法对于此trie很方便,但启用它需要 IEnumerable 的虚拟实现才能进行编译。 CLR似乎没有调用GetEnumerator(),我建议您不要尝试枚举其结果。
using System;
using System.Collections.Generic;
using System.Linq; // only used in Main()
class Program
{
// trie with payload of type <String>
static Trie<String> value_trie = new Trie<String>
{
{ "rabbit", "cute" },
{ "giraffe", "tall" },
{ "ape", "smart" },
{ "hippo", "large" },
};
// degenerate case of a trie without payload
static Trie<bool> simple_trie = new Trie<bool>
{
{ "rabbit", true },
{ "giraffe", true },
{ "ape", true },
{ "hippo", true },
};
static void Main(String[] args)
{
String s = "Once upon a time, a rabbit met an ape in the woods.";
// Retrieve payloads for words in the string.
//
// output:
// cute
// smart
foreach (String word in value_trie.AllSubstringValues(s))
Console.WriteLine(word);
// Simply test a string for any of the words in the trie.
// Note that the Any() operator ensures that the input is no longer
// traversed once a single result is found.
//
// output:
// True
Console.WriteLine(simple_trie.AllSubstringValues(s).Any(e=>e));
s = "Four score and seven years ago.";
// output:
// False
Console.WriteLine(simple_trie.AllSubstringValues(s).Any(e => e));
}
}
class TrieNode<TValue>
{
private TrieNode<TValue>[] nodes = null;
private TValue m_value = default(TValue);
private Char m_base;
public Char Base { get { return m_base; } }
public bool IsEnd { get { return !m_value.Equals(default(TValue)); } }
public TValue Value
{
get { return m_value; }
set { m_value = value; }
}
public IEnumerable<TrieNode<TValue>> Nodes { get { return nodes; } }
public TrieNode<TValue> this[char c]
{
get
{
if (nodes != null && m_base <= c && c < m_base + nodes.Length)
return nodes[c - m_base];
return null;
}
}
public TrieNode<TValue> AddChild(char c)
{
if (nodes == null)
{
m_base = c;
nodes = new TrieNode<TValue>[1];
}
else if (c >= m_base + nodes.Length)
{
Array.Resize(ref nodes, c - m_base + 1);
}
else if (c < m_base)
{
Char c_new = (Char)(m_base - c);
TrieNode<TValue>[] tmp = new TrieNode<TValue>[nodes.Length + c_new];
nodes.CopyTo(tmp, c_new);
m_base = c;
nodes = tmp;
}
TrieNode<TValue> node = nodes[c - m_base];
if (node == null)
{
node = new TrieNode<TValue>();
nodes[c - m_base] = node;
}
return node;
}
};
class Trie<TValue> : System.Collections.IEnumerable
{
private TrieNode<TValue> _root = new TrieNode<TValue>();
// This dummy enables C# 3.0 initialization syntax
public System.Collections.IEnumerator GetEnumerator()
{
return null;
}
public void Add(String s, TValue v)
{
TrieNode<TValue> node = _root;
foreach (Char c in s)
node = node.AddChild(c);
node.Value = v;
}
public bool Contains(String s)
{
TrieNode<TValue> node = _root;
foreach (Char c in s)
{
node = node[c];
if (node == null)
return false;
}
return node.IsEnd;
}
public TValue Find(String s_in)
{
TrieNode<TValue> node = _root;
foreach (Char c in s_in)
{
node = node[c];
if (node == null)
return default(TValue);
}
return node.Value;
}
public IEnumerable<TValue> FindAll(String s_in)
{
TrieNode<TValue> node = _root;
foreach (Char c in s_in)
{
node = node[c];
if (node == null)
break;
if (node.Value != null)
yield return node.Value;
}
}
public IEnumerable<TValue> AllSubstringValues(String s)
{
int i_cur = 0;
while (i_cur < s.Length)
{
TrieNode<TValue> node = _root;
int i = i_cur;
while (i < s.Length)
{
node = node[s[i]];
if (node == null)
break;
if (node.Value != null)
yield return node.Value;
i++;
}
i_cur++;
}
}
};
答案 4 :(得分:2)
这是正确的语法:
if(names.Contains(myName))
{
//success code//
}
答案 5 :(得分:1)
if (names.Contains(myName))
{
//success code//
}