Source for de.webdings.tools.StringSearch

   1: /* Stringsearch.java - Copyright (c) 2005 by Stefan Thesing
   2:  <p>This file is part of Webdings Tools.</p>
   3:  <p>Webdings Tools is free software; you can redistribute it and/or modify
   4:  it under the terms of the GNU General Public License as published by
   5:  the Free Software Foundation; either version 2 of the License, or
   6:  (at your option) any later version.</p>
   7: <p>Webdings Tools is distributed in the hope that it will be useful,
   8: but WITHOUT ANY WARRANTY; without even the implied warranty of
   9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10: GNU General Public License for more details.</p>
  11: <p>You should have received a copy of the GNU General Public License
  12: along with Webdings Tools; if not, write to the<br>
  13: Free Software Foundation, Inc.,<br>
  14: 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA<br>
  15: */
  16: package de.webdings.tools;
  17: 
  18: /**
  19:  * StringSearch offers some basic search functions with Strings.
  20:  * 
  21:  * @author Stefan Thesing<br>
  22:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  23:  * @version 1.0 23.05.2005
  24:  */
  25: public class StringSearch {
  26:     /**
  27:      * Returns <code>true</code> if <code>s</code> contains 
  28:      * <code>search</code> as a substring. StringSearch 
  29:      * is not case sensitive.
  30:      * 
  31:      * @param s String that is searched
  32:      * @param search String that is searched for
  33:      * @return <code>true</code> if <code>s</code> contains <code>search</code>
  34:      */
  35:     public static boolean stringContains(String s, String search) {
  36:         //Case1: search has the same length as s
  37:         if(search.length() == s.length()) {
  38:             return s.equalsIgnoreCase(search);
  39:         }
  40:         //Case2: search is shorter than s.
  41:         //Compare all possible substrings of s that have the same
  42:         //length as search, to search
  43:         if(search.length() < s.length()) {
  44:             String sub;
  45:             for(int i=0; i<=s.length()-search.length(); ++i) {
  46:                 sub = s.substring(i, i+search.length());
  47:                 if(sub.equalsIgnoreCase(search)){
  48:                     return true;
  49:                 }
  50:             }
  51:         }
  52:         //Case3: search is longer than s, ergo: s cannot contain search
  53:         return false;
  54:     }
  55:     
  56:     /**
  57:      * Does the same as {@link #stringContains(String, String) stringContains()},
  58:      * but it's case sensitive.
  59:      * @param s String that is searched
  60:      * @param search String that is searched for
  61:      * @return <code>true</code> if <code>s</code> contains <code>search</code>
  62:      */
  63:     public static boolean stringContainsCaseSensitive(String s, String search) {
  64: //        Case1: search has the same length as s
  65:         if(search.length() == s.length()) {
  66:             return s.equals(search);
  67:         }
  68:         //Case2: search is shorter than s.
  69:         //Compare all possible substrings of s that have the same
  70:         //length as search, to search
  71:         if(search.length() < s.length()) {
  72:             String sub;
  73:             for(int i=0; i<=s.length()-search.length(); ++i) {
  74:                 sub = s.substring(i, i+search.length());
  75:                 if(sub.equals(search)){
  76:                     return true;
  77:                 }
  78:             }
  79:         }
  80:         //Case3: search is longer than s, ergo: s cannot contain search
  81:         return false;
  82:     }
  83: }

© 2005 by Stefan Thesing;
Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.