您的位置:首页 > 编程语言 > Java开发

Java String.indexOf() 函数用法小结

2013-10-23 16:27 351 查看
1. indexOf的参数是 String, startIndex: Number;
indexOf的返回值为int,

2. Function indexOf 包含如下几个格式:
1). Strng.indexOf(substring) //搜索String中的substring,默认从0位开始;
2). String.indexOf(substring, int m) //搜索String中的substring, 默认从第m位开始;

Sample:
取IP地址的第一个代码段:

int p;
int q;
String inputIP = null;
String IPsection1 = null;
String IPsection2 = null;

inputIP = "1.100.1.1";
p = inputIP.indexOf('.');
q = inputIP.indexOf('.',p+1);
IPsection1 = inputIP.substring(0,p);
IPsection2 = inputIP.substring(p+1, q);
System.out.println("the 1st IP section is "+IPsection1);
System.out.println("the 2nd IP section is "+IPsection2);
输出结果为
the 1st IP section is 1
the 2nd IP section is 100
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: