目录
public Stirng():创建一个空白字符串,不含任何内容。
public String(char[]array)根据字符数组的内容,来创建对应的字符串
public String(byte[]array)根据字节数组的内容,来创建对应的字符串
1、public boolean equals(Object obj):
2、public boolean equalsIgnoreCase(String str):忽略大小写,进行内容比较
public String concat(String str):将当前字符串和参数字符串拼接成为 新的字符串
public char charAt(int index):获取指定索引位置的单个字符(索引从0开始)
public int indexOf(String str):查找参数字符串在本字符串中首次出现的索引位置,如果没有,返回-1值
public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串
public String substring(int begin,int end):截取从begin开始,一直到end结束,中间的字符串
public char[] toCharArray():将当前字符串拆分为字符数组作为返回值
public byte[] getBytes():获得当前字符串底层的字节数组
public String replace(charSequence old,CharSequence new):将所有出现的老字符串替换成为新的字符串,返回替换之后的结果字符串
public String[] split(String regex):按照参数的规则,将字符串切分成为若干部分
导包:
import java.lang.String(可以不用写)
程序中所有双引号的字符串,都是String类的对象
字符串的特点:
1、字符串的内容永不改变【重点】
2、因为字符串不可改变,所以字符串是可以共享使用的
3、字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组。
创建字符串的常见3+1方式:
三种构造方法:
public Stirng():创建一个空白字符串,不含任何内容。
public String(char[]array)根据字符数组的内容,来创建对应的字符串
public String(byte[]array)根据字节数组的内容,来创建对应的字符串
一种直接创建:
字符串常量池:
程序当中直接写上的双引号字符串,就在字符串常量池中
对于基本类型来说,==是进行数值的比较
对于引用类型来说,==是进行地址值的比较
字符串常用方法:
字符串比较方法:
==是进行对象地址值的比较,如果确实需要字符串的内容比较,可以使用两个方法:
1、public boolean equals(Object obj):
参数可以是任何对象,只有参数是一个字符串并且内容相同的才会给true,否则返回false
注意:
1、任何对象都能用object接收
2、equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样
3、如果比较双方一个常量一个变量,推荐写法:常量.equals(变量);防止NullPointer
package String;
public class equal {
public static void main(String[] args) {
String s1="hello";
String s2="hello";
char[] charArray={'h','e','l','l','o'};
String s3=new String(charArray);
System.out.println(s1.equals(s2));//true
System.out.println(s2.equals(s3));//true
}
}
2、public boolean equalsIgnoreCase(String str):忽略大小写,进行内容比较
package String;
public class equal {
public static void main(String[] args) {
String s1="hello";
String s2="Hello";
System.out.println(s1.equalsIgnoreCase(s2));//true
}
}
字符串获取方法:
public int length():获取字符串长度
String s1="111";
int length=s1.length();//3
public String concat(String str):将当前字符串和参数字符串拼接成为 新的字符串
String s1="hello";
String s2="world";
String s3=s1.concat(s2);//s3=helloworld,s1s2不变
public char charAt(int index):获取指定索引位置的单个字符(索引从0开始)
String s="hello";
char c=s.charAt(1);//e
public int indexOf(String str):查找参数字符串在本字符串中首次出现的索引位置,如果没有,返回-1值
String s1="helloworld";
int index=s1.indexOf("llo");//2
int index1=s1.index0f("Ll0");//-1
字符串截取方法:
public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串
String s1="helloworld";
String s2=s1.substring(5);//world
public String substring(int begin,int end):截取从begin开始,一直到end结束,中间的字符串
String s1="helloworld";
String s2=s1.substring(4,7);//owo
字符串的转换方法:
public char[] toCharArray():将当前字符串拆分为字符数组作为返回值
public byte[] getBytes():获得当前字符串底层的字节数组
public String replace(charSequence old,CharSequence new):将所有出现的老字符串替换成为新的字符串,返回替换之后的结果字符串
备注:CharSequence为接口,可以接受String类型
char []chars="hello".toCharArray();//转换成为字符数组
chars.length;//5
byte[] bytes="abc".getBytes();//转换成为字节数组
//97 98 99
String s1="how do you do";
Strings2=s1.replace("0","*");//h*w d* y*u d*
字符串的分割方法:
public String[] split(String regex):按照参数的规则,将字符串切分成为若干部分
String s1="aaa,bbb,ccc";
String[]array1=s1.split(",");
//aaa bbb ccc
String s2="aaa bbb ccc";
String[] array2=s2.split(" ");
//aaa bbb ccc
注意:
参数为“正则表达式”:(不能以”.“分割)