Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Java program that checks if a String array contains a particular string value. Check the blogpost at https://coderolls.com/java-check-if-array-contains-value-or-element/
import java.util.Arrays;
/**
* A Java program that checks if a String array contains a particular string value
*
* @author Gaurav Kukade
*
*/
public class CheckInStringArray {
public static void main(String[] args) {
String [] names = {"gaurav", "shubham", "krishna", "mayur"};
String checkForString = "mayur";
boolean stringPresentInArray = Arrays.asList(names).contains(checkForString);
if(stringPresentInArray) {
System.out.println("String value '"+checkForString+"' is present in names Array.");
}else {
System.out.println("String value '"+checkForString+"' is not present in names Array.");
}
}
//Output:
//String value 'mayur' is present in names Array.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment