How to search a string in a String array

I need to search a string in an array of strings. I don't want to use any for looping in it

string [] arr = {"One","Two","Three"}; string theString = "One" 

I need to check if var is present in arr.

+64
Nov 05 '08 at 12:12
source share
13 answers

Each method that was mentioned earlier executes a loop both inside and outside, so it doesn't matter how to implement it. Here is another example of finding all target line links

  string [] arr = {"One","Two","Three"}; var target = "One"; var results = Array.FindAll(arr, s => s.Equals(target)); 
+60
Nov 05 '08 at 13:56
source share

Well, you have to look for something, and the loop is more efficient than recursion (since tail recursion is not fully implemented) ... so if you just don’t want to loop yourself, then any of

 bool has = arr.Contains(var); // .NET 3.5 

or

 bool has = Array.IndexOf(arr, var) >= 0; 

For reference: avoid names like var is a keyword in C # 3.0.

+111
Nov 05 '08 at 12:16
source share

Should there be a string []? A List <String> will give you what you need.

 List<String> testing = new List<String>(); testing.Add("One"); testing.Add("Two"); testing.Add("Three"); testing.Add("Mouse"); bool inList = testing.Contains("Mouse"); 
+24
Nov 05 '08 at 12:18
source share
 bool exists = arr.Contains("One"); 
+11
Nov 05 '08 at
source share

I think it's better to use Array.Exists than Array.FindAll.

+9
Mar 26 '10 at 11:53
source share

It is pretty simple. I always use this code to search for a string from an array of strings

 string[] stringArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; int pos = Array.IndexOf(stringArray, value); if (pos > -1) { return true; } else { return false; } 
+8
Nov 06 '13 at 10:15
source share

If the array is sorted, you can use BinarySearch . This operation is O (log n), so it is faster than cyclic. If you need to apply multiple searches and speed, you can sort it (or a copy) before using it.

+5
Nov 05 '08 at
source share

Each class that implements IList has a Contains method (object value) . And the System.Array system.

+4
Nov 05 '08 at 12:18
source share

Why is the ban "I do not want to use any cycle"? This is the most obvious solution. If you have a chance to be obvious, take it!

Please note that calls like arr.Contains(...) will still be circular, it will not be the one who wrote the loop.

Do you find an alternative view that is more suitable for search?

  • A good set implementation will work well. (HashSet, TreeSet or local equivalent).
  • If you can be sure that arr will be sorted, you can use a binary search (which will need to be rewritten or quoted, but not as often as a direct linear search).
+4
Nov 05 '08 at 12:57
source share

In the first shot, I could come up with something like this (but this is pseudocode and assuming that you cannot use any of the built-in .NET libraries). It may take a little tweaking and rethinking, but it should be good enough for a start, maybe?

 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); 
+2
Nov 05 '08 at
source share

In C #, if you can use an ArrayList, you can use the Contains method, which returns boolean:

 if MyArrayList.Contains("One") 
+2
Nov 05 '08 at
source share

You can use the Find method of an array type. From .NET 3.5 and higher.

 public static T Find<T>( T[] array, Predicate<T> match ) 

Here are some examples:

 // we search an array of strings for a name containing the letter "a": static void Main() { string[] names = { "Rodney", "Jack", "Jill" }; string match = Array.Find (names, ContainsA); Console.WriteLine (match); // Jack } static bool ContainsA (string name) { return name.Contains ("a"); } 

Here is the same code, abbreviated anonymous method:

 string[] names = { "Rodney", "Jack", "Jill" }; string match = Array.Find (names, delegate (string name) { return name.Contains ("a"); } ); // Jack 

The lambda expression shortens it further:

 string[] names = { "Rodney", "Jack", "Jill" }; string match = Array.Find (names, n => n.Contains ("a")); // Jack 
+2
Nov 07 '14 at 2:09
source share

You can check for the existence of an element on

 arr.Any(x => x == "One") 
+1
Dec 05 '14 at 8:55
source share



All Articles