Skip to Content

Java String Array Contains Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.         Java String Array Contains Example
  3.         This Java String Array Contains example shows how to find a String in
  4.         String array in Java.
  5.  */
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class StringArrayContainsExample {
  10.        
  11.         public static void main(String args[]){
  12.                
  13.                 //String array
  14.                 String[] strMonths = new String[]{"January", "February", "March", "April", "May"};
  15.                
  16.                 //Strings to find
  17.                 String strFind1 = "March";
  18.                 String strFind2 = "December";
  19.                
  20.                 /*
  21.                  * There are several ways we can check whether a String array
  22.                  * contains a particular string.
  23.                  *
  24.                  * First of them is iterating the array elements and check as given below.
  25.                  */
  26.                
  27.                 boolean contains = false;
  28.                
  29.                 //iterate the String array
  30.                 for(int i=0; i < strMonths.length; i++){
  31.                        
  32.                         //check if string array contains the string
  33.                         if(strMonths[i].equals(strFind1)){
  34.  
  35.                                 //string found
  36.                                 contains = true;
  37.                                 break;
  38.                                
  39.                         }
  40.                 }
  41.                
  42.                 if(contains){
  43.                         System.out.println("String array contains String " + strFind1);
  44.                 }else{
  45.                         System.out.println("String array does not contain String " + strFind1);
  46.                 }
  47.                
  48.                 /*
  49.                  * Second way to check whether String array contains a string is to use
  50.                  * Arrays class as given below.
  51.                  */
  52.                
  53.                 contains = Arrays.asList(strMonths).contains(strFind1);
  54.                 System.out.println("Does String array contain " + strFind1 + "? " + contains);
  55.                
  56.                 contains = Arrays.asList(strMonths).contains(strFind2);
  57.                 System.out.println("Does String array contain " + strFind2 + "? " + contains);
  58.  
  59.         }
  60. }
  61.  
  62. /*
  63. Output of above given Java String Array Contains example would be
  64. String array contains String March
  65. Does String array contain March? true
  66. Does String array contain December? false
  67. */