title: Java: Contains() method
body: <p>Trying to implement <code>contains()</code> method without using built-in method <code>contains()</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public static boolean containsCS(String str, CharSequence cs) {
      //char[] chs = str.toCharArray();
      boolean result = false;
  
      int i=0;
      while(i&lt;str.length()) {
          int j=0;
          while(j&lt;cs.length()) {
              if(NEED TO CHECK IF THERE IS AN INDEX OUT OF BOUNDS EXCEPTION) {
                  result = false;
                  break;
              }
              if(str.charAt(i+j)==cs.charAt(j)) {
                  result|=true; //result = false or true -&gt;&gt;&gt;&gt;&gt; which is true.
                  j++;
              } else {
                  result = false;
                  break;
              }
          }
          i++;
      }
      return false;
  }
  </code></pre>
  
  <p>Let's say:</p>
  
  <pre><code>String   str = "llpll"
  Charsequence cs = "llo"
  </code></pre>
  
  <p>I want to make sure this method works properly in the above case where the <code>Charsequence</code> has one or more <code>char</code> to check but the <code>String</code> runs out length. How should I write the first <code>if</code> statement?</p>