当前位置:编程学习 > JSP >>

java indexOf() 和lastIndexOf()字符查找实现方法

java indexof() 和lastindexof()字符查找实现方法

class indexofdemo {
  public static void main(string args[]) {
    string s = "now is the time for all good men " + "to come to the aid of their country.";

    system.out.println(s);
    system.out.println("indexof(t) = " + s.indexof('t'));
    system.out.println("lastindexof(t) = " + s.lastindexof('t'));
    system.out.println("indexof(the) = " + s.indexof("the"));
    system.out.println("lastindexof(the) = " + s.lastindexof("the"));
    system.out.println("indexof(t, 10) = " + s.indexof('t', 10));
    system.out.println("lastindexof(t, 60) = " + s.lastindexof('t', 60));
    system.out.println("indexof(the, 10) = " + s.indexof("the", 10));
    system.out.println("lastindexof(the, 60) = " + s.lastindexof("the", 60));
  }
}

引用上面的实例我们来做个字符串查找实例

public class main {

  /**
   * find the latest index of any of a set of potential substrings.
   *
   * a <code>null</code> string will return <code>-1</code>.
   * a <code>null</code> search array will return <code>-1</code>.
   * a <code>null</code> or zero length search array entry will be ignored,
   * but a search array containing "" will return the length of <code>str</code>
   * if <code>str</code> is not null. this method uses {@link string#indexof(string)}
   *
   * <pre>
   * stringutils.lastindexofany(null, *)                   = -1
   * stringutils.lastindexofany(*, null)                   = -1
   * stringutils.lastindexofany(*, [])                     = -1
   * stringutils.lastindexofany(*, [null])                 = -1
   * stringutils.lastindexofany("zzabyycdxx", ["ab","cd"]) = 6
   * stringutils.lastindexofany("zzabyycdxx", ["cd","ab"]) = 6
   * stringutils.lastindexofany("zzabyycdxx", ["mn","op"]) = -1
   * stringutils.lastindexofany("zzabyycdxx", ["mn","op"]) = -1
   * stringutils.lastindexofany("zzabyycdxx", ["mn",""])   = 10
   * </pre>
   *
   * @param str  the string to check, may be null
   * @param searchstrs  the strings to search for, may be null
   * @return the last index of any of the strings, -1 if no match
   */
  public static int lastindexofany(string str, string[] searchstrs) {
      if ((str == null) || (searchstrs == null)) {
          return -1;
      }
      int sz = searchstrs.length;
      int ret = -1;
      int tmp = 0;
      for (int i = 0; i < sz; i++) {
          string search = searchstrs[i];
          if (search == null) {
              continue;
          }
          tmp = str.lastindexof(search);
          if (tmp > ret) {
              ret = tmp;
          }
      }
      return ret;
  }
  // ----------------------------------------------------------------------
  /**
   * checks if an array of objects is empty or <code>null</code>.
   *
   * @param array  the array to test
   * @return <code>true</code> if the array is empty or <code>null</code>
   * @since 2.1
   */
  public static boolean isempty(char[] array) {
      if (array == null || array.length == 0) {
          return true;
      }
      return false;
  }
  // empty checks
  //-----------------------------------------------------------------------
  /**
   * checks if a string is empty ("") or null.
   *
   * <pre>
   * stringutils.isempty(null)      = true
   * stringutils.isempty("")        = true
   * stringutils.isempty(" ")       = false
   * stringutils.isempty("bob")     = false
   * stringutils.isempty("  bob  ") = false
   * </pre>
   *
   * note: this method changed in lang version 2.0.
   * it no longer trims the string.
   * that functionality is available in isblank().
   *
   * @param str  the string to check, may be null
   * @return <code>true</code> if the string is empty or null
   */
  public static boolean isempty(string str) {
      return str == null || str.length() == 0;
  }

}

实例二

public static int indexofany(string str, string[] searchstrs) {
      if ((str == null) || (searchstrs == null)) {
          return -1;
      }
      int sz = searchstrs.length;

      // string's can't have a max_valueth index.
      int ret = integer.max_value;

      int tmp = 0;
      for (int i = 0; i < sz; i++) {
          string search = searchstrs[i];
          if (search == null) {
              continue;
          }
          tmp = str.indexof(search);
          if (tmp == -1) {
              continue;
          }

          if (tmp < ret) {
              ret = tmp;
          }
      }

      return (ret == integer.max_value) ? -1 : ret;
  }

测试

* stringutils.indexofany(null, *)                     = -1
   * stringutils.indexofany(*, null)                     = -1
   * stringutils.indexofany(*, [])                       = -1
   * stringutils.indexofany("zzabyycdxx", ["ab","cd"])   = 2
   * stringutils.indexofany("zzabyycdxx", ["cd","ab"])   = 2
   * stringutils.indexofany("zzabyycdxx", ["mn","op"])   = -1
   * stringutils.indexofany("zzabyycdxx", ["zab","aby"]) = 1
   * stringutils.indexofany("zzabyycdxx", [""])          = 0
   * stringutils.indexofany("", [""])                    = 0
   * stringutils.indexofany("", ["a"])                   = -1

补充:Jsp教程,Java基础 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,