Created
May 17, 2021 15:20
-
-
Save gauravkukade/1914b7274cd5017364e78d2e6bc5f5bb to your computer and use it in GitHub Desktop.
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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