How to check if a string is in an array

I am trying to check whether a string is in an array and if continues even though the fileInfo.Name.Contains a string that is in files.Any:

                        **if (files.Any((fileInfo.Name.Contains)))** 
                         fileInfo.Name, fileInfo.Length, 

If you alread have the filenames collected in an array, then you should either do it this way:

if (files.Any() && files.Contains(fileInfo.Name))
   // Do something

If you just want to check if a file exists then you can use File.Exists:

    // Do Something

Java - Check if Array contains a certain value?, Java examples to check if an Array (String or Primitive type) contains a certain values, updated with Java 8 stream APIs. You can find string in an array using two methods. (I know there are plenty of methods, but two methods are most common) Method 1 You can loop each item of an array and compare each item of the array with the items that are to find.

So you have a collection of full file paths? And you want to check if one or more of those list entries match with a specific file name?

Perhaps this would work for you:

string fileToSearch = "DATABASE.MDB";
bool found = files.Any(fileName => new FileInfo(fileName).Name.ToUpper() == fileToSearch.ToUpper());


An alternative to constructing new FileInfo objects would be to use System.IO.Path:

bool found = files.Any(fileName => Path.GetFileName(fileName).ToUpper() == fileToSearch.ToUpper());

Edit 2:

On the other hand, if you want to search for a specific file name, and you want to use the result, you could do something like this:

var fileToSearch = "DATABASE.MDB";

var fileInfo =
    (from f in files
     let fi = new FileInfo(f)
     where fi.Name.ToUpper() == fileToSearch.ToUpper()
     select fi).FirstOrDefault();

if (fileInfo != null)
    if (fileInfo.Exists)
        Console.WriteLine($"{fileInfo.Name} ({fileInfo.Length} bytes).");
        Console.WriteLine($"{fileInfo.Name} (does not exist).");

I used a LINQ query here for readability. You could use the extension methods (files.Select(f => new FileInfo(f)).Where(fi => fi.Name.ToUpper() == fileToSearch.ToUpper()).FirstOrDefault()) as well, but that's up to you.

How do I determine whether an array contains a particular value in , String[] values = {"AB","BC","CD","AE"}; boolean contains = Arrays. stream(values). anyMatch("s"::equals); To check whether an array of int , double or long contains a value use IntStream , DoubleStream or LongStream respectively. 1.2 Example to check if a String Array contains multiple values : package com.mkyong.core; import java.util.Arrays; import java.util.List; public class StringArrayExample2 { public static void main(String[] args) { String[] alphabet = new String[]{"A", "C"}; // Convert String Array to List List<String> list = Arrays.asList(alphabet); // A or B if (list.contains("A") || list.contains("B")) { System.out.println("Hello A or B"); } // A and B if (list.containsAll(Arrays.

if (Array.Exists(files, element => element.Contains(fileInfo.Name)))

How to Check if an Array Contains a Value in Java Efficiently?, Four Different Ways to Check If an Array Contains a Value. 1) Using List : public static boolean useList(String[] arr, String targetValue) { return Arrays. Item is in the array" End If End Sub Function IsInArray (stringToBeFound As String, arr As Variant) As Boolean IsInArray = UBound (Filter (arr, stringToBeFound)) > -1 End Function The above routine correctly gives the message "Yes! The item is in the array"

Check if a Java Array Contains a Value, String[] seedArray(int length) {. String[] strings = new String[length];. Random value = new Random();. for (int i = 0; i < length; i++) {. strings[i]� When you run the program, the output will be: Four is found. In the above program, we've used a non-primitive data type String and used Arrays 's stream () method to first convert it to a stream and anyMatch () to check if array contains the given value toFind.

JavaScript Array includes() Method, This method returns true if the array contains the element, and false if not. Note: The Check if an array includes "Banana", starting the search at position 3:. * Second way to check whether String array contains a string is to use * Arrays class as given below.

How to Check if Java Array Contains a Value?, We will go over the array elements using the for loop and use the equals() method to check if the array element is equal to the given value. String[] vowels = { "A"� This MATLAB function returns 1 (true) if A is a string array. Select a Web Site. Choose a web site to get translated content where available and see local events and offers.

  • @PrashantPimpale no, you can use files.Any() also to check if there is any data in the array, because this method returns bool.
  • Yup got it! I thought it was related to fileName exists or file exists related!