String类
字符串的功能
转换功能:
1.public byte[] getBytes:使用平台默认的字符集将此String编码为byte序列并存储到一个新的byte数组中;
2.public byte[] getBytes(Charset charset):使用给定的charset将此String编码到byte序列,并存储到新的byte数组中,charset是编码集,如果平台默认编码集是GBK则不需要声明;
3.public String replace(char oldChar,char newChar):将字符串的一部分转换为另一部分,输出新的字符串;
4.public char[] toCharArray():将此字符串转换为一个新的字符数组;
5.public String toLowerCase():使用默认语言环境的规则将字符串中所有的字母转换为小写;
6.public String toUpperCase():使用默认语言环境的规则将字符串中所有的字母转换为大写;
7.public String toString():返回字符串本身;
8.public String trim():返回字符串副本,忽略前导空白和尾部空白;
9.public static String valueOf(XXX数据类型 变量名):返回XXX数据类型参数的字符串表现形式(将XXX数据类型转换为字符串形式)
10.public void getChars(
int srcBegin,字符串中要复制到第一个字符的索引
int secEnd,字符串要复制到最后一个字符的索引
char[] dst,目标数组
int dstBegin 目标数组中的起始偏移量
)
执行方法后会将该字符数组改变
截取功能:
- public String substring(int beginIndex):从开始位置截取默认截取到末尾,返回一个新的字符串;
- public String substring(int beginIndex,int endIndex):从开始位置截取,截取到指定位置结束(包左不包右),返回一个新的字符串;
拼接功能:
- 字符串1=字符串2+字符串3
字符串变量相加是先在栈内存开辟空间,再看方法区中的常量池中是否有这个元素,有则指向该对象,没有则开辟空间创建这个对象,指向该对象;
字符串常量相加,是先拼接,再看方法区中的常量池中是否有这个元素,有则指向该对象,没有则开辟空间创建这个对象,指向该对象
- public String concat(String str): 将指定位置的字符串拼接到此字符串的末尾,返回一个新的字符串
判断功能:
- public boolean contains(CharSequence s):可以把CharSequence暂时理解为字符串,当大字符串中包含这个小字符串时,返回true;
- public boolean endsWith(String suffix):判断此字符串是否以指定的后缀结束;
- public boolean equals(Object anObject):将此字符串与指定字符串比较,如果值相等则返回true.当两个字符串都为null时比较,编译器会报错;
- public boolean equalsIgnoreCase(String anotherString):将此字符串与指定字符串忽略大小写比较比较;
- public boolean isEmpty():
只有当字符串长度为0时,才返回true;
- public boolean startWith(String prefix):测试此字符串是否以指定字符串开始
- public boolean startWith(String prefix,int toffset):
toffset表示指定字符串从toffset索引开始截取, 如果参数表示的字符序列是此对象从索引 toffset 处开始的子字符串前缀,则返回 true
比较功能:
- public int compareTo(String anotherString):按字典顺序比较两个字符串
根据源码简单理解:如果每个索引处的值相同,则输出两个字符串的长度的差值;如果索引处的值不同,则输出第一次发现值不同时的两个值的差值;
2.public int compareToIgnoreCase(String str)
按字典顺序比较,不考虑大小写
返回索引或者索引处的值的功能:
- public char charAt(int index)
返回指定索引处的char值
- public int codePointAt(int index)
返回指定索引处的字符,返回的是Unicode码值(ASCLL码表对应的值)
- public int codePointCount(int beginIndex,int ndIndex)
返回此String的指定文本范围内中的Unicode代码的个数(包左不包右)
- public int indexOf(XXX)
返回指定字符或者字符串第一次出现的索引
- public int indexOf(XXX,int index)
从指定索引处开始搜索,返回指定的字符或者字符串第一次出现的索引
- public int lastIndexOf(XXX)
返回指定字符或者字符串最后一次出现的索引
- public int lastIndexOf(XXX,int index)
从指定索引处开始搜索,返回指定的字符或者字符串第一次出现的索引
其他方法:
- public int hashcode()
返回此字符串的哈希码值
- public int length()
返回此字符串的长度,长度等于字符串中Unicode代码单元的数量
练习
1.字符串的反转:
反转方法--->
public static String reverse(String str){
char[] ch=str.toCharArray(); //将字符串转换为字符数组,下来进行遍历
String string="";
for (int i = ch.length-1; i >=0; i--) {
string+=ch[i];
}
return string;
}
2.求字符串中有多少个大写字母,多少个小写字母,多少个数字?
public static void method(String string){
char[] ch=string.toCharArray();
int bigcount=0;
int smallcount=0;
int numcount=0;
for (int i = 0; i < ch.length; i++) {
if (ch[i]>='a'&&ch[i]<='z') {
smallcount++;
}else if(ch[i]>'A'&&ch[i]<='Z') {
bigcount++;
}else if(ch[i]>='0'&&ch[i]<='9'){
numcount++;
}
}
System.out.println("大写字符:"+bigcount);
System.out.println("小写字符:"+smallcount);
System.out.println("数字字符:"+numcount);
}
3.按照指定格式将数组拼接成一个字符串
int[] arr={1,2,3} ===>String类型的[1,2,3]
public static String method(int[] arr){
String string="";
string+="[";
for (int i = 0; i < arr.length; i++) {
if (i==arr.length-1) {
string+=arr[i];
string+="]";
}else {
string+=arr[i];
string+=",";
}
}
return string;
}
核心是字符串的拼接
补充
编码和解码:
编码:String--->byte[]--->二进制(编码格式:必须一致)
解码:二进制--->byte[]--->String(解码格式:必须一致)
注意
字符串变量相加,是先开辟空间(不是先相加),再看常量池中是否有这个字符串
字符串常量相加,是先拼接(先相加),再开辟空间