目录
int indexOf(int ch,int fromIndex)
int indexOf(String str,int fromIndex)
String substring(int start,int end)
点击下方链接学习更多的String类 API深入研究
字符串String API深入研究1.0
Sring类的判断功能 API深入研究2.0
String类的字符串转换功能 API深入研究4.0
String类的替换功能、去除字符串两端空格、按字典顺序比较两个字符串 API深入研究5.0
在我们企业的日常开发中,String类是每天都会去使用的高频类,那么我们来深入探索一下。(个人参考API文档编写,仅供学习参考)
字符串:简单理解,就是由多个字符组成的数据,叫做字符串;也可以看作是一个字符数组。
观察API发现:
1、String代表的是字符串,属于java.lang下面的。所以使用的时候不需要导包。
2、String类代表字符串,Java程序中的所有字符串文字(例如"abc"),都被实现为此类的实例(对象)。
3、字符串不变:它们的值在创建后不能被更改,字符串是常量,一旦被赋值,就不能改变。
注意事项:String类重写了toString()方法。
String类的获取功能:
int length()
查看API文档我们知道:
返回此字符串的长度。 长度等于字符串中的数字Unicode code units 。
Specified by:
结果
由该对象表示的字符序列的长度。
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//int length() 获取字符串的长度
System.out.println(s.length());
}
}
10
Process finished with exit code 0
char charAt(int index)
查看API文档我们知道:
返回char
指定索引处的值。 指数范围为0
至length() - 1
。 该序列的第一个char
值在索引0
,下一个索引为1
,依此类推,与数组索引一样。
如果char
由索引指定的值是surrogate ,则返回所述替代值。
Specified by:
参数
index
- char
值的指数。
结果
所述char
此字符串的指定索引处的值。 第一个char
值是索引0
。
异常
IndexOutOfBoundsException
- 如果 index
参数为负数或不小于此字符串的长度。
顾名思义就是返回char字符指定位置的索引值,索引从0开始到length()-1
参考代码1:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//char charAt(int index) 返回char字符指定位置索引的值,索引从0开始到length()-1
System.out.println(s.charAt(7));
System.out.println(s.charAt(0));
}
}
r
hProcess finished with exit code 0
这里我们有提到异常,那什么情况下会出现异常呢?
参考代码2:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//char charAt(int index) 返回char字符指定位置索引的值,索引从0开始到length()-1
System.out.println(s.charAt(7));
System.out.println(s.charAt(0));
//StringIndexOutOfBoundsException
System.out.println(s.charAt(100));
}
}
当我们不小心将length()-1的值写的大于字符串的长度,将会出现字符串截取下标越界异常。
int indexOf(int ch)
查看API文档我们知道:
返回指定字符第一次出现的字符串内的索引。 如果与值的字符ch
在此表示的字符序列发生String
第一事件发生之对象,则索引(在Unicode代码单元)被返回。 对于从0到0xFFFF(含)范围内的ch
的值,这是最小值k ,使得:
this.charAt(k) == ch
是真的。 对于ch
其他值,它是最小值k ,使得:
this.codePointAt(k) == ch
是真的。 在这两种情况下,如果此字符串中没有此类字符,则返回-1
。
参数
ch
- 一个字符(Unicode代码点)。
结果
在通过该对象表示的字符序列的字符的第一次出现的索引,或 -1
如果字符不会发生。
顾名思义:
就是返回指定字符第一次出现的字符串内的索引,如果此字符串中没有此类字符,则返回-1
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//int indexOf(int ch)
//返回指定字符第一次出现的字符串内的索引,如果此字符串中没有此类字符,则返回-1 。
System.out.println(s.indexOf('l'));
System.out.println(s.indexOf(97));
}
}
2
-1Process finished with exit code 0
int indexOf(String str)
查看API文档我们知道:
返回指定子字符串第一次出现的字符串内的索引。
返回的索引是其中的最小值k :
this.startsWith(str, k)
如果k的值不存在,则返回-1
。
参数
str
- 要搜索的子字符串。
结果
指定的子串,或第一次出现的索引 -1
如果不存在这样的发生。
顾名思义:
就是返回的是字符串中第一个字符在大字符串中的索引值,如果k的值不存在,则返回-1.
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//int indexOf(String str)
//返回的是字符串第一个字符在大字符串中的索引值,如果k的值不存在,则返回-1
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("qwer"));
}
}
4
-1Process finished with exit code 0
int indexOf(int ch,int fromIndex)
查看API文档我们知道:
返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索。 对于从0到0xFFFF(含)范围内的ch
的值,返回的索引是最大值k ,使得:
(this.charAt(k) == ch)&&
(k <= fromIndex)
是真的。 对于ch
其他值,它是最大的值k ,使得:
(this.codePointAt(k) == ch)&&
(k <= fromIndex)
是真的。 在这两种情况下,如果此字符串在位置fromIndex
之前或之后没有出现这样的字符,则返回-1
。
所有索引在char
值(Unicode代码单位)中char
。
参数
ch
- 一个字符(Unicode代码点)。
fromIndex
- 开始搜索的索引。 对fromIndex的价值没有fromIndex
。 如果它大于或等于该字符串的长度,它具有与等于该字符串长度小于1的效果相同的效果:可以搜索整个字符串。 如果它是负的,它具有与-1:-1相同的效果。
结果
在通过该对象,它是小于或等于表示的字符序列的字符的最后一次出现的索引 fromIndex
,或 -1
如果该字符不该点之前发生。
顾名思义:
就是返回指定字符第一次出现在字符串中的索引,以指定的索引开始搜索,如果不存在或者输入的fromIndex的长度大于字符串的长度将返回-1.
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//int indexOf(int ch,int fromIndex)
//返回指定字符第一次出现在字符串中的索引,以指定的索引开始搜索,如果不存在或者fromIndex长度大于字符串的长度将返回-1
System.out.println(s.indexOf('l',4));
System.out.println(s.indexOf('p',4));
System.out.println(s.indexOf('l',40));
System.out.println(s.indexOf('p',40));
}
}
8
-1
-1
-1Process finished with exit code 0
int indexOf(String str,int fromIndex)
查看API文档我们知道:
返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。
返回的索引是其中的最小值k :
k >= fromIndex&&
this.startsWith(str, k)
如果不存在这样的k值,则返回-1
。
参数
str
- 要搜索的子字符串。
fromIndex
- 从 fromIndex
开始搜索的索引。
结果
指定的字符串的第一个出现的索引,开始指定索引处,或 -1
如果不存在这样的发生。
顾名思义:
就是返回指定字符串第一次出现在字符串中的索引,以指定的索引开始搜索,如果不存在或者输入的fromIndex的长度大于字符串的长度将返回-1.
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//int indexOf(String str,int fromIndex)
//返回指定字符串第一次出现在字符串中的索引,以指定的索引开始搜索,如果不存在或者输入的fromIndex的长度大于字符串的长度将返回-1.
System.out.println(s.indexOf("or",4));
System.out.println(s.indexOf("hel",5));
System.out.println(s.indexOf("elo"));
}
}
6
-1
-1Process finished with exit code 0
注意;如果只写了str:" "字符,没有写formIndex的值,结果也返回-1
String substring(int start)
查看API文档我们知道:
返回一个字符串,该字符串是此字符串的子字符串。 子字符串以指定索引处的字符开头,并扩展到该字符串的末尾。
例子:
"unhappy".substring(2) returns "happy" "Harbison".substring(3) returns "bison" "emptiness".substring(9) returns "" (an empty string)
参数
beginIndex
- 开始索引(含)。
结果
指定的子字符串。
异常
IndexOutOfBoundsException
- 如果 beginIndex
为负或大于此 String
对象的长度。
顾名思义:
就是返回的是一个字符串,该字符串是此字符串的子字符串,字符串从指定的索引处开始,并扩展到字符拆的末尾,包含开始索引位置的值。
参考代码1:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//String substring(int start)
/*
返回的是一个字符串,该字符串是此字符串的子字符串,子字符串从指定的索引处开始,
并扩展到字符拆的末尾,包含开始索引位置的值,如果beginIndex为负或大于此String对象的长度
则会出现异常IndexOutOfBoundsException。
*/
System.out.println(s.substring(3));
System.out.println(s.substring(7));
System.out.println(s.substring(10));
}
}
loworld
rld
Process finished with exit code 0
参考代码2:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//String substring(int start)
/*
返回的是一个字符串,该字符串是此字符串的子字符串,子字符串从指定的索引处开始,
并扩展到字符拆的末尾,包含开始索引位置的值,如果beginIndex为负或大于此String对象的长度
则会出现异常IndexOutOfBoundsException。
*/
System.out.println(s.substring(3));
System.out.println(s.substring(7));
System.out.println(s.substring(10));
System.out.println(s.substring(-2));
}
}
参考代码3:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//String substring(int start)
/*
返回的是一个字符串,该字符串是此字符串的子字符串,子字符串从指定的索引处开始,
并扩展到字符拆的末尾,包含开始索引位置的值,如果beginIndex为负或大于此String对象的长度
则会出现异常IndexOutOfBoundsException。
*/
System.out.println(s.substring(3));
System.out.println(s.substring(7));
System.out.println(s.substring(10));
// System.out.println(s.substring(-2));
System.out.println(s.substring(11));
}
}
从参考代码2和参考代码3可以看出,如果beginIndex
为负或大于此 String
对象的长度,则会
IndexOutOfBoundsException字符串截取下标越界异常。
String substring(int start,int end)
查看API文档我们知道:
返回一个字符序列,该序列是该序列的子序列。
调用此方法的形式
str.subSequence(begin, end)
行为与调用完全相同
str.substring(begin, end)
Specified by:
API Note:
该方法定义为String
类可以实现CharSequence接口。
参数
beginIndex
- 开始索引,包括。
endIndex
- 结束索引,排他。
结果
指定的子序列。
异常
IndexOutOfBoundsException
- 如果 beginIndex
或 endIndex
为负数,如果 endIndex
大于 length()
,或者如果 beginIndex
大于 endIndex
顾名思义:
就是返回的是一个字符串,该字符串是此字符串的字字符串,字符串开始于start位置,并截取到end-1的位置,左闭右开[,)含头不含尾。
参考代码1:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//String substring(int start,int end)
//返回的是一个字符串,该字符串是此字符串的子字符串,字串开始于start位置,并截取到end-1的位置,左闭右开 [,) 含头不含尾
System.out.println(s.substring(5,10));
System.out.println(s.substring(3,5));
}
}
world
loProcess finished with exit code 0
参考代码2:
public class testStringDemo1 {
public static void main(String[] args) {
String s = "helloworld";
//String substring(int start,int end)
//返回的是一个字符串,该字符串是此字符串的子字符串,字串开始于start位置,并截取到end-1的位置,左闭右开 [,) 含头不含尾
System.out.println(s.substring(5,10));//world
System.out.println(s.substring(3,5));
//StringIndexOutOfBoundsException
System.out.println(s.substring(1,20));
}
}
从参考代码2可以看出如果 beginIndex
或 endIndex
为负数,如果 endIndex
大于 length()
,或者如果 beginIndex
大于 endIndex则会
IndexOutOfBoundsException字符串截取下标越界异常。
注意该文章里的字符和字符串不要搞混淆了!
到底啦!给靓仔一个关注吧!