Java的索引of (方法
1、方法介绍
(1) index of (intch )返回指定字符在此字符串中出现的第一个索引
)2) indexof(stringstr )返回指定子字符串在此字符串中出现的第一个索引
)3) indexof(intch,int fromIndex )返回此字符串中指定字符第一次出现的索引,并从指定索引开始搜索
)4) indexof(stringstr,int fromIndex )返回指定索引中指定子字符串在此字符串中出现的第一个索引
2、方法实例
//*
* @Title:IndexOf.java
* @Package:com.you.data
* @Description:Java indexOf ()
* @Author:游海东
* @date: 2014年3月17日下午7:31:58
* @版本v1.2. 3
*/
包com.you.data;
//*
* @类名:IndexOf
* @说明:1,公共索引of (intch ) )。
*返回指定字符在此字符串中出现的第一个索引
*如果值出现在此String对象表示的字符串中
*对于ch字符,返回该字符第一次出现的索引。 以Unicode代码单位表示。
*对于0到0xffff (包括0和0xffff )范围内的ch值,返回值为
*this.Charat(k )==ch
* 2,indexof(stringstr )返回指定子字符串在此字符串中出现的第一个索引
* 3、indexof(intch,int fromIndex )返回此字符串中指定字符第一次出现的索引,并从指定索引开始搜索。
* 4,indexof(stringstr,int fromIndex )返回从指定索引开始的指定子字符串在此字符串中第一个出现的索引。
* @Author :游海东
* @date: 2014年3月17日下午7:31:58
*/
公共类索引of {
//*
* @Title : main
* @Type : IndexOf
* @date : 2014年3月17日下午7:31:58
* @Description :
* @param args
*/
publicstaticvoidmain (字符串[ ] args ) )。
{
//*
*声明字符串str
*/
String str='hjhfd? 7c8yu? 90uij? b$%a$^%^dnjdhf? 有吗? ndjfhdj? abdeefsdfdf? ojkdjk837587485 ';
//*
*最初d出现的位置
*/
intstrnum1=str.indexof(100;
//*
*打印显示第一个d的位置
*/
System.out.println (第一个索引of:' strnum1);
(system.out.println(((n )-------------------- ) ) ) ) ) ) ) ) ) ) ) ) )
//*
*第一个a出现的位置
*/
intstrnum2=str.indexof('a );
//*
*打印第一个a出现的位置
*/
System.out.println (第二索引of:' strnum2);
(system.out.println(((n )-------------------- ) ) ) ) ) ) ) ) ) ) ) ) )
//*
*第一个b出现的位置
*/
int strnum3=str.index of (99,2 );
//*
*打印第一个b出现的位置
*/
system.out.println(3 (第三个索引of:' strnum3);
(system.out.println(((n )-------------------- ) ) ) ) ) ) ) ) ) ) ) ) )
//*
*第一个b出现的位置
*/
intstrnum4=str.indexof('b ',34 );
//*
*打印第一个b出现的位置
*/
system.out.println(4 (第四个索引of:' strnum4);
}
}
3、实例结果
第一个索引of:4
-------------请参阅
第二个索引of:21
-------------请参阅
第三个索引of:7
-------------请参阅
第四个索引of:46