黑马程序员技术交流社区
标题: String类的lastIndexOf()方法 [打印本页]
作者: 山水游客 时间: 2012-6-21 08:18
标题: String类的lastIndexOf()方法
本帖最后由 孙胜录 于 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,为什么会这样呢?
作者: 李天甲 时间: 2012-6-21 08:27
本帖最后由 李天甲 于 2012-6-21 08:40 编辑
空字符串和空格不太一样吧...
如果是空格的话,那么当然位置就是6.但是空字符串的话一般是第一个或者最后一个.
str.indexOf("") 和str.lastIndexOf("")分别返回的是0,和最大值.- /**
- * Code shared by String and StringBuffer to do searches. The
- * source is the character array being searched, and the target
- * is the string being searched for.
- *
- * @param source the characters being searched.
- * @param sourceOffset offset of the source string.
- * @param sourceCount count of the source string.
- * @param target the characters being searched for.
- * @param targetOffset offset of the target string.
- * @param targetCount count of the target string.
- * @param fromIndex the index to begin searching from.
- */
- static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
- char[] target, int targetOffset, int targetCount,
- int fromIndex) {
- /*
- * Check arguments; return immediately where possible. For
- * consistency, don't check for null str.
- */
- int rightIndex = sourceCount - targetCount;
- if (fromIndex < 0) {
- return -1;
- }
- if (fromIndex > rightIndex) {
- fromIndex = rightIndex;
- }
- /* Empty string always matches. */
- if (targetCount == 0) {
- return fromIndex;
- }
- int strLastIndex = targetOffset + targetCount - 1;
- char strLastChar = target[strLastIndex];
- int min = sourceOffset + targetCount - 1;
- int i = min + fromIndex;
- startSearchForLastChar:
- while (true) {
- while (i >= min && source[i] != strLastChar) {
- i--;
- }
- if (i < min) {
- return -1;
- }
- int j = i - 1;
- int start = j - (targetCount - 1);
- int k = strLastIndex - 1;
- while (j > start) {
- if (source[j--] != target[k--]) {
- i--;
- continue startSearchForLastChar;
- }
- }
- return start - sourceOffset + 1;
- }
- }
复制代码 上面是jdk查找lastIndexOf的代码,如果你调试一下就可以看到,当被查找字段为("");时,传递进去的被查找的对象也就是 @param target the characters being searched for.是一个字符数组char[].而这个值是空也就是说char[] target等于空数组...
在非空里面查找空数组,自然返回的是第一个或者最后一个..呵呵
作者: 陆强强 时间: 2012-6-21 08:29
本帖最后由 陆强强 于 2012-6-21 08:36 编辑
("")中间少个空格,
作者: 淡然 时间: 2012-6-21 08:41
- package hcy.test.main;
- class Test
- {
- public static void main(String[] args)
- {
- String str = "We are students";
- int size = str.lastIndexOf(" ");//而我预想当中的结果是空字符串在字符串str中索引位置是:6,为什么会这样呢?
- //空格和空字符串不是一样
- System.out.println("空字符串在字符串str中索引位置是: "+size);
- System.out.println("字符串str的长度是: "+str.length());
- }
- }
复制代码 输出结果:
空字符串在字符串str中索引位置是: 6
字符串str的长度是: 15
// int size = str.lastIndexOf(" ");//双引号中间加个空格
//而我预想当中的结果是空字符串在字符串str中索引位置是:6,为什么会这样呢?
//根据你的意思好像你是要判断最后一个空格在字符串中的位置吧,把空字符串改成一个空格的字符串就行了
作者: 常佳杰 时间: 2012-6-21 08:58
哥们看你的代码
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...
作者: 山水游客 时间: 2012-6-21 09:07
懂了,谢谢大家啊!!
作者: 姚玉鹏 时间: 2012-6-21 09:08
""查找的是空字符 “ ”中间有空格 才是你要查找的
作者: 逝去的记忆ヽ 时间: 2012-6-21 09:10
本帖最后由 逝去的记忆ヽ 于 2012-6-21 09:15 编辑
首先理解含义,
String lastIndexOf方法:指定字符串在此实例中的最后一个匹配项的索引位置。
索引编号从零开始。也就是说,字符串中第一个字符的位置为索引 0,而最后一个字符的位置为 length -1; 搜索从该实例的最后一个字符位置开始,沿反向向前搜索,直到找到 value 或检查完第一个字符位置为止。
此方法使用当前区域性执行单词(区分大小写和区域性)搜索。
那么你那个代码当中搜索的字符串中 你的输入是lastIndexOf("");这里面是空的, 而不是空格。
所以你的那个输出结果是索引字符串最后一个位置的角标 15的位置。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |