A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 山水游客 中级黑马   /  2012-6-21 08:18  /  2717 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 孙胜录 于 2012-6-21 09:07 编辑

功能:使用lastIndexOf()方法查看字符串str中空字符串的位置,然后输出字符串的长度。代码
public lastIndexOfDemo
{
        public stativ void main(String[] args)
{
    String str = "We are students";
     int   size = str.lastIndexOf("");
     System.out.println("空字符串在字符串str中索引位置是: "+size);
        System.out.println("字符串str的长度是: "+str.length());
     }
}
打印出的结果是
空字符串在字符串str中索引位置是:15
字符串str的长度是:15
而我预想当中的结果是空字符串在字符串str中索引位置是:6,为什么会这样呢?

7 个回复

倒序浏览
本帖最后由 李天甲 于 2012-6-21 08:40 编辑

空字符串和空格不太一样吧...
如果是空格的话,那么当然位置就是6.但是空字符串的话一般是第一个或者最后一个.
str.indexOf("") 和str.lastIndexOf("")分别返回的是0,和最大值.
  1.     /**
  2.      * Code shared by String and StringBuffer to do searches. The
  3.      * source is the character array being searched, and the target
  4.      * is the string being searched for.
  5.      *
  6.      * @param   source       the characters being searched.
  7.      * @param   sourceOffset offset of the source string.
  8.      * @param   sourceCount  count of the source string.
  9.      * @param   target       the characters being searched for.
  10.      * @param   targetOffset offset of the target string.
  11.      * @param   targetCount  count of the target string.
  12.      * @param   fromIndex    the index to begin searching from.
  13.      */
  14.     static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
  15.                            char[] target, int targetOffset, int targetCount,
  16.                            int fromIndex) {
  17.         /*
  18.          * Check arguments; return immediately where possible. For
  19.          * consistency, don't check for null str.
  20.          */
  21.         int rightIndex = sourceCount - targetCount;
  22.         if (fromIndex < 0) {
  23.             return -1;
  24.         }
  25.         if (fromIndex > rightIndex) {
  26.             fromIndex = rightIndex;
  27.         }
  28.         /* Empty string always matches. */
  29.         if (targetCount == 0) {
  30.             return fromIndex;
  31.         }

  32.         int strLastIndex = targetOffset + targetCount - 1;
  33.         char strLastChar = target[strLastIndex];
  34.         int min = sourceOffset + targetCount - 1;
  35.         int i = min + fromIndex;

  36.     startSearchForLastChar:
  37.         while (true) {
  38.             while (i >= min && source[i] != strLastChar) {
  39.                 i--;
  40.             }
  41.             if (i < min) {
  42.                 return -1;
  43.             }
  44.             int j = i - 1;
  45.             int start = j - (targetCount - 1);
  46.             int k = strLastIndex - 1;

  47.             while (j > start) {
  48.                 if (source[j--] != target[k--]) {
  49.                     i--;
  50.                     continue startSearchForLastChar;
  51.                 }
  52.             }
  53.             return start - sourceOffset + 1;
  54.         }
  55.     }
复制代码
上面是jdk查找lastIndexOf的代码,如果你调试一下就可以看到,当被查找字段为("");时,传递进去的被查找的对象也就是 @param   target       the characters being searched for.是一个字符数组char[].而这个值是空也就是说char[] target等于空数组...
在非空里面查找空数组,自然返回的是第一个或者最后一个..呵呵
回复 使用道具 举报
本帖最后由 陆强强 于 2012-6-21 08:36 编辑

("")中间少个空格,

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

回复 使用道具 举报
  1. package hcy.test.main;

  2. class  Test
  3. {
  4.         public static void main(String[] args)
  5.         {

  6.                     String str = "We are students";
  7.                      int   size = str.lastIndexOf(" ");//而我预想当中的结果是空字符串在字符串str中索引位置是:6,为什么会这样呢?
  8.                                                                                              //空格和空字符串不是一样
  9.                      System.out.println("空字符串在字符串str中索引位置是: "+size);
  10.                         System.out.println("字符串str的长度是: "+str.length());

  11.         }
  12. }
复制代码
输出结果:
空字符串在字符串str中索引位置是: 6
字符串str的长度是: 15

// int   size = str.lastIndexOf(" ");//双引号中间加个空格
//
而我预想当中的结果是空字符串在字符串str中索引位置是:6,为什么会这样呢?

//根据你的意思好像你是要判断最后一个空格在字符串中的位置吧,把空字符串改成一个空格的字符串就行了

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

回复 使用道具 举报
哥们看你的代码
public lastIndexOfDemo
{
        public stativ void main(String[] args)//写代码一定要细心,static也写错了,写代码往往就是写错单词这样的小错误,导致自己很郁闷不知道那出问题了
{
    String str = "We are students";
     int   size = str.lastIndexOf("");//这""号里应该有空格的!
     System.out.println("空字符串在字符串str中索引位置是: "+size);
        System.out.println("字符串str的长度是: "+str.length());
     }
}
后边加上空格:
打印出的结果是
空字符串在字符串str中索引位置是:6
字符串str的长度是:15

要想得到字符串的长度先把空格去掉,
使用String对象的方法replaceAll
replaceAll(String regex, String replacement)
        regex表示要被替换的,replacement表示要替换后的
示例:
public class replace {
public static void main(String args[]) {
  String str="We are students";
  String stra=str.replaceAll(" ", "");
  System.out.println(stra.length());
}
}
这样打印出来的字符串长度是13...

评分

参与人数 1技术分 +1 收起 理由
黄奕豪 + 1 赞一个!

查看全部评分

回复 使用道具 举报
懂了,谢谢大家啊!!
回复 使用道具 举报
""查找的是空字符  “ ”中间有空格 才是你要查找的
回复 使用道具 举报
本帖最后由 逝去的记忆ヽ 于 2012-6-21 09:15 编辑

首先理解含义,
String lastIndexOf方法:指定字符串在此实例中的最后一个匹配项的索引位置。

索引编号从零开始。也就是说,字符串中第一个字符的位置为索引 0,而最后一个字符的位置为 length -1;
搜索从该实例的最后一个字符位置开始,沿反向向前搜索,直到找到 value 或检查完第一个字符位置为止。
此方法使用当前区域性执行单词(区分大小写和区域性)搜索。

那么你那个代码当中搜索的字符串中 你的输入是lastIndexOf("");这里面是空的,  而不是空格。
所以你的那个输出结果是索引字符串最后一个位置的角标 15的位置。



回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马