C# 如何在字符串数组中搜索字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/264962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search a string in String array
提问by balaweblog
I need to search a string in the string array. I dont want to use any for looping in it
我需要在字符串数组中搜索一个字符串。我不想在其中使用任何循环
string [] arr = {"One","Two","Three"};
string theString = "One"
I need to check whether theString variable is present in arr.
我需要检查 arr 中是否存在 theString 变量。
采纳答案by Tamir
Every method, mentioned earlier does looping either internally or externally, so it is not really important how to implement it. Here another example of finding all references of target string
前面提到的每个方法都会在内部或外部循环,因此如何实现它并不重要。这是查找目标字符串的所有引用的另一个示例
string [] arr = {"One","Two","Three"};
var target = "One";
var results = Array.FindAll(arr, s => s.Equals(target));
回答by Marc Gravell
Well, something is going to have to look, and looping is more efficient than recursion (since tail-end recursion isn't fully implemented)... so if you just don't want to loop yourself, then either of:
嗯,有些事情必须要看看,循环比递归更有效(因为尾端递归没有完全实现)......所以如果你不想自己循环,那么:
bool has = arr.Contains(var); // .NET 3.5
or
或者
bool has = Array.IndexOf(arr, var) >= 0;
For info: avoid names like var- this is a keyword in C# 3.0.
信息:避免使用 var 之类的名称- 这是 C# 3.0 中的关键字。
回答by ZombieSheep
Does it have to be a string[] ? A List<String> would give you what you need.
它必须是一个 string[] 吗?一个 List<String> 会给你你需要的。
List<String> testing = new List<String>();
testing.Add("One");
testing.Add("Two");
testing.Add("Three");
testing.Add("Mouse");
bool inList = testing.Contains("Mouse");
回答by VolkerK
Each class implementing IList has a method Contains(Object value). And so does System.Array.
每个实现 IList 的类都有一个方法Contains(Object value)。System.Array 也是如此。
回答by mohammedn
bool exists = arr.Contains("One");
回答by GvS
If the array is sorted, you can use BinarySearch. This is a O(log n) operation, so it is faster as looping. If you need to apply multiple searches and speed is a concern, you could sort it (or a copy) before using it.
如果数组已排序,则可以使用BinarySearch。这是一个 O(log n) 操作,因此它比循环更快。如果您需要应用多个搜索并且速度是一个问题,您可以在使用它之前对其进行排序(或副本)。
回答by Salman Kasbati
At first shot, I could come up with something like this (but it's pseudo code and assuming you cannot use any .NET built-in libaries). Might require a bit of tweaking and re-thinking, but should be good enough for a head-start, maybe?
一开始,我可以想出这样的东西(但它是伪代码,假设您不能使用任何 .NET 内置库)。可能需要一些调整和重新思考,但应该足以领先一步,也许吧?
int findString(String var, String[] stringArray, int currentIndex, int stringMaxIndex)
{
if currentIndex > stringMaxIndex
return (-stringMaxIndex-1);
else if var==arr[currentIndex] //or use any string comparison op or function
return 0;
else
return findString(var, stringArray, currentIndex++, stringMaxIndex) + 1 ;
}
//calling code
int index = findString(var, arr, 0, getMaxIndex(arr));
if index == -1 printOnScreen("Not found");
else printOnScreen("Found on index: " + index);
回答by DOK
In C#, if you can use an ArrayList, you can use the Contains method, which returns a boolean:
在 C# 中,如果可以使用 ArrayList,则可以使用 Contains 方法,该方法返回一个布尔值:
if MyArrayList.Contains("One")
回答by bendin
Why the prohibition "I don't want to use any looping"? That's the most obvious solution. When given the chance to be obvious, take it!
为什么禁止“我不想使用任何循环”?这是最明显的解决方案。当有机会变得明显时,抓住它!
Note that calls like arr.Contains(...)
are still going to loop, it just won't be you who has written the loop.
请注意,像这样的调用arr.Contains(...)
仍然会循环,只是编写循环的人不会是你。
Have you considered an alternate representation that's more amenable to searching?
您是否考虑过更易于搜索的替代表示?
- A good Set implementation would perform well. (HashSet, TreeSet or the local equivalent).
- If you can be sure that
arr
is sorted, you could use binary search (which would need to recurse or loop, but not as often as a straight linear search).
- 一个好的 Set 实现会表现得很好。(HashSet、TreeSet 或本地等效项)。
- 如果可以确定
arr
已排序,则可以使用二进制搜索(这需要递归或循环,但不像直线搜索那样频繁)。
回答by Joe Cheri Ross
I think it is better to use Array.Existsthan Array.FindAll.
我认为这是更好地使用Array.Exists比Array.FindAll。