字符串
java程序中的所有字符串字面值(如"abc")都作为此类的实例实现。
翻译:程序当中所有双引号字符串,都是String类的对象。(就算没有new,也照样是)
字符串的特点
- 字符串的内容永不可变。
- 正是因为字符串不可变,所以字符串是可以共享使用的
- 字符串效果上相当于char[]字符数组,但是底层原理是byte[]字节数组
创建字符串
- 三种构造方法
- public String(),创建一个空白字符串,不包含任何内容。
- public String(char[] array); 根据字符数组的内容,来创建对应的字符串。
- public String(byte[] array); 根据字节数组的内容,来创建对应的字符串。
- 直接创建
String str = “hello”; //直接创建
注意:直接写上双引号,即使没new也是字符串对象
public class Demo {
public static void main(String[] args) {
//使用空参构造
String str1 = new String(); //小括号留空,说明什么内容也没有
System.out.println("第一个字符串:" + str1);
//使用字符数组创建字符串
char[] charArray = { 'A', 'B', 'c'};
String str2 = new String(charArray);
System.out.println("第二个字符串:" + str2);
//使用字节数组创建字符串
byte[] byteArray = { 97, 98, 99};
String str3 = new String(byteArray);
System.out.println("第三个字符串:" + str3);
//直接创建
String str4 = "hello";
System.out.println("第四个字符串:" + str4);
}
}
字符串的常量池
字符串常量池:程序当中直接写上双引号的字符串,就在字符串常量池中。
对于基本类型来说,==
是进行值比较
对于引用类型来说,==
是进行地址比较
字符串的常用方法
public boolean equals(Object obj){} //参数可以是任何对象,只有参数是一个字符串并且内容相同时才会给true,否则返回false。
public boolean equalsIgnoreCase(String str): 忽略大小写,进行内容比较
注意事项:
- 任何对象都可以使用Object进行接收
- 如果比较双方一个常量一个变量,推荐把常量字符串写在前面
如:“abc”.equals(str);
public class Demo {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
char[] charArray = { 'H', 'e', 'l', 'l', 'o' };
String str3 = new String(charArray);
System.out.println(str1.equals(str2)); //true
System.out.println(str2.equals(str3)); //true
System.out.println(str3.equals("Hello")); //true
System.out.println("Hello".equals(str1)); //true
System.out.println("hello".equals(str1)); //flase 区分大小写
System.out.println("hello".equalsIgnoreCase(str1)); //true 不区分大小写
}
}
与获取相关的方法
public int length(); 获取字符串当中含有的字符个数;
public String concat(String Str); 将当前字符串和参数字符串拼接成为返回值新的字符串
public char charAt(int index); 获取指定索引位置的单个字符。(索引值从0开始)
public int indexOf(String str); 查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1;
使用示例:
public class Demo {
public static void main(String[] args) {
//获取字符串长度
int length = "hello,world!".length();
System.out.println("字符串的长度是:" + length); //12
//拼接字符串
String str1 = "hello";
String str2 = "world";
String str3 = str1.concat(str2);
System.out.println(str1); //hello
System.out.println(str2); //world
System.out.println(str3); //helloworld
//获取指定索引位置的单个字符
char ch = "hello".charAt(1);
System.out.println(ch); // e
// 查找参数字符串在本字符串当中首次出现的索引位置
// 如果没有返回 -1
String original = "HelloWorld";
int index = original.indexOf("llo");
System.out.println(index); // 2
System.out.println("HelloWorld".indexOf("abc")); //-1
}
}
字符串的截取方法
public String substring(int index); 截取从参数位置一直到字符串末尾,返回新的字符串。
public String substring(int begin,int end); 截取从begin开始,一直到end结束。
备注:[begin,end)–包含左边,不包含右边
字符串的转换方法
public char[] toCharArray(); 将当前字符串拆分成字符数组作为返回值
public byte[] getBytes(); 获得当前字符串底层的字节数组。
public String replace(CharSequence oldString, CharSequence newString);
将所有出现的老字符串替换成为新的字符串,返回替换之后的结果
备注:CharSequence意思就是说可以接受字符串类型
public class Demo {
public static void main(String[] args) {
//转换成为字符数组
char[] cahrs = "Hello".toCharArray();
//转换成为字节数组
byte[] bytes = "abc".getBytes();
for (int i=0 ; i<bytes.length ; i++) {
System.out.println(bytes[i]);
}
String str = "How do you do?";
String str1 = str.replace("o","*");
System.out.println(str); //How do you do?
System.out.println(str1); //H*w d* y*u d*?
}
}
分割字符串
public String[] split(String regex); 按照参数规则,将字符串切分成为若干部分。
注意事项:
split方法的参数其实是一个正则表达式,如果按照英文句点“.”进行切分,必须写“\.”(两个反斜杠)
public static void main(String[] args) {
String str1 = "aaa,bb,c";
String[] array1 = str1.split(",");
for (int i=0 ; i<array.length ; i++) {
System.out.print(array1[i] + " "); //aaa bb c
}
}
static静态关键字
使用static关键字修饰的内容,就不在属于对象自己,而是属于类的,所以凡是本类的对象,都共享一份