String contains



  • I came across this code, written by a customer.

    I imagine this must be some kind of optimisation to avoid the use of String.indexOf  or StringUtils.contains...

        /**
         * true is the source contains the pattern
         * 
         * @param source
         * @param pattern
         * @return boolean
         */
        public static boolean contains(final String source, final String pattern) {
            if (source == null || pattern == null) {
                return false;
            }
    
        if (source.length() < pattern.length()) {
            return false;
        }
    
        // We use <= to manage the case : source = pattern
        for (int i = 0; i <= source.length() - pattern.length(); i++) {
            String tmp = source.substring(i, pattern.length() + i);
            if (tmp.compareTo(pattern) == 0) {
                return true;
            }
        }
        return false;
    }
    


  • /*
    * true is the source contains the pattern
    */

    That's comment GOLD



  • @Welbog said:

    /*
    * true is the source contains the pattern
    */

    That's comment GOLD

    I think I got that in a fortune cookie once.



  • You people know about the Pattern?!

    I must inform the Council at once! We must contact the Source immediately! 



  • //Returns true when evaluates to true.



  • So I guess the rule is: don't try to optimize unless you're sure of everything that's going on under the hood.  I'm guessing it didn't cross the author's mind that String.indexOf will accomplish the same thing.  Gotta love the substring() call inside the loop though - so funny.  I know I shouldn't laugh, but it's great how the exact opposite of optimization is what's being done here.



  • @DOA said:

    You people know about the Pattern?!

    I must inform the Council at once! We must contact the Source immediately! 

    No, THAT's comment gold. Get up again from the ground



  • @Quinnum said:

    @Welbog said:
    /*
    * true is the source contains the pattern
    */

    That's comment GOLD

    I think I got that in a fortune cookie once.

     
    That would be a FORTRAN cookie.



  • @vax said:

    I imagine this must be some kind of optimisation to avoid the use of String.indexOf  or StringUtils.contains...

     Some kind of optimisation... this code would wreak havoc with your performance if let loose on a source of 10k times "a", and a pattern of 5k "a" followed by a single "b". And, yes, such source/pattern pairs can be handled more efficiently, (most probably) by the methods referred to in other posts...
     



  • my favorite thing is that it's using the word pattern, which makes me thing regular expression...



  • @tster said:

    my favorite thing is that it's using the word pattern, which makes me thing regular expression...

    For the record, regexp-based methods are wrong for needle/haystack string searches. The right way is usually Knuth-Morris-Pratt (or in some cases, Boyer-Moore).



  • @bouk said:

    @vax said:

    I imagine this must be some kind of optimisation to avoid the use of String.indexOf  or StringUtils.contains...

     Some kind of optimisation... this code would wreak havoc with your performance if let loose on a source of 10k times "a", and a pattern of 5k "a" followed by a single "b". And, yes, such source/pattern pairs can be handled more efficiently, (most probably) by the methods referred to in other posts...

    That's why I said "some kind"... It was ironic ;-) 


Log in to reply