day14 Java 常用类 String类(一)


在我最最难过的时候
给我温暖的眼神




字符串:
就是由多个字符组成的一串数据叫字符串
也可以看作一个字符数组
是一个常量,一旦初始化,其值就无法改变



一、构造方法

1.1 public String()

创造一个长度为0的空字符串,一般无意义

String s1 = new String();
System.out.println(s1);
System.out.println(s1 + " + " + s1.length());
//	 + 0

1.2 public String(byte[] bytes)

将一个字节数组转成一个字符串
由于需要将数组内转换成ascii码,所以数组内成员要合法

byte[] a = {
    
    97, 98, 99, 100, 101};
String s2 = new String(a);
System.out.println(s2 + " + " + s2.length());
//	abcde + 5

1.3 public String(byte[] bytes,int offset,int length)

将字节数组中的一部分截取出来变成一个字符串
offset 是起始索引位置,length 是截取长度

byte[] b = {
    
    97, 98, 99, 100, 101};
String s3 = new String(a,1,3);
System.out.println(s3 + " + " + s3.length());
//	bcd + 3

1.4 public String(char[] value)

将一个字符数组转化成一个字符串

char[] c = {
    
    'f','g','h','韭','菜','盒','子'};
String s4 = new String(c);
System.out.println(s4 + " + " + s4.length());
//	fgh韭菜盒子 + 7

1.5 public String(char[] value,int offset,int count)

将字符数组中的一部分截取出来作为一个字符串
offset 是起始索引位置,count 是截取长度

char[] d = {
    
    'f','g','h','韭','菜','盒','子'};
String s5 = new String(d,3,4);
System.out.println(s5 + " + " + s5.length());
//	韭菜盒子 + 4

1.6 public String(String original)

将一个字符串转换为新字符串

String e = "fgh韭菜盒子";
String s6 = new String(e);
System.out.println(s6 + " + " + s6.length());
//	fgh韭菜盒子 + 7



二、 关于 String 值无法改变

String s = "hello";
s += "world";
System.out.println(s);		
//	helloworld

此时输出的是 helloworld ,原因是:
这里的变量 s 是引用类型,指向的是常量池中 “hello” 的地址
当第二步对它作 += 操作时,会在常量池中再开辟空间储存 “helloworld”
再将 s 指向 “helloworld” 的地址
请添加图片描述



二、String 和 new String 的区别

String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1 == s2);		//	false 
System.out.println(s1.equals(s2));		//	ture

s1 指向的是常量池中 “hello” 的地址
s2 是通过堆内存中创建的对象,指向常量池中 “hello” 的地址
二者一个是常量池的地址,一个是堆内存的地址,所以地址不同
String 类中默认重写了 equals() 方法,此时比较的是二者最终常量池中的地址
请添加图片描述



三、字符串相加

1、字符串如果是变量相加,一定会先开辟空间,然后再拼接
2、字符串如果是常量相加,是先加,然后再去常量池里找,如果找到了就返回,如果找不到就创建

String a = "hello";
String b = "world";
String c = "helloworld";
String d = a + b;
String e = "helloworld";
String f = a + b;
//获取字符串地址值
System.out.println(System.identityHashCode(c));		//	1163157884
System.out.println(System.identityHashCode(d));		//	1956725890
System.out.println(System.identityHashCode(e));		//	1163157884
System.out.println(System.identityHashCode(f));		//	356573597

System.out.println(d == a + b);		//false
System.out.println(d == "helloworld");		//false



四、String 类的判断功能

4.1 boolean equals(Object obj)

比较字符串中的内容是否相同 区分大小写

String a = "helloworld";
String b = "helloworld";
System.out.println(a.equals(b));		//	true

4.2 boolean equalsIgnoreCase(String str)

比较字符串中的内容是否相同 忽略大小写

String a = "helloworld";
String c = "Helloworld";
System.out.println(a.equalsIgnoreCase(c));		//	true

4.3 boolean contains(String str)

判断大的字符串中是否包含字符串 str,如果包含,返回的是true,否则就是false
区分大小写

String a = "helloworld";
String d = "world";
System.out.println(a.contains(d));		//	true

4.4 boolean startsWith(String str)

测试此字符串是否以指定的前缀开头
区分大小写

String a = "helloworld";
String e = "hel";
String f = "ello";
System.out.println(a.startsWith(e));		//	true
System.out.println(a.startsWith(f));		//	fales

## 4.5 boolean endsWith(String str) >测试此字符串是否以指定的后缀结束 >区分大小写 ```java String a = "helloworld"; String e = "rld"; String f = "Rld"; System.out.println(a.startsWith(e)); // true System.out.println(a.startsWith(f)); // false ```

4.6 boolean isEmpty()

判断字符串是否为空

String g = "";
String h = null;
System.out.println(g == h);		//	false
System.out.println(g.isEmpty());		//	true
System.out.println(h.isEmpty());		//	true



五、String 类的获取功能

5.1 int length()

获取字符串的长度

String s = "helloworld";
System.out.println(s.length());		//	10

5.2 char charAt(int index)

返回char指定索引处的值

String s = "helloworld";
System.out.println(s.charAt(1));		//	e
//越界会报错

5.3 int indexOf(int ch)

返回指定字符第一次出现的字符串内的索引

String s = "helloworld";
System.out.println(s.indexOf('e'));		//	1
//找不到返回 -1

5.4 int indexOf(String str)

返回指定子字符串第一次出现的字符串内的索引
字符串第一个字符索引

String s = "helloworld";
System.out.println(s.indexOf("ello"));		//	1
//找不到返回 -1

5.5 int indexOf(int ch,int fromIndex)

返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索

String s = "helloworld";
System.out.println(s.indexOf('o',4));		//	4
//找不到返回 -1

5.6 int indexOf(String str,int fromIndex)

返回指定字符串第一次出现的字符串内的索引,以指定的索引开始搜索

String s = "helloworld";
System.out.println(s.indexOf("wo",3));		//	5
//找不到返回 -1

5.7 String substring(int start)

返回一个字符串,该字符串是此字符串的子字符串
子字符串以指定索引处的字符开头,并扩展到该字符串的末尾

String s = "helloworld";
System.out.println(s.substring(5));		//	world
//越界会报错

5.8 String substring(int start,int end)

返回一个字符串,该字符串是此字符串的子字符串
子串开始于指定索引 start 并延伸到索引 end - 1
左闭右开 [ start,end )

String s = "helloworld";
System.out.println(s.substring(5,10));		//	world
//越界会报错



总结

boolean equals(Object obj)

比较字符串中的内容是否相同
区分大小写

boolean contains(String str)

判断大的字符串中是否包含字符串 str,如果包含,返回的是true,否则就是false
区分大小写

boolean startsWith(String str)

测试此字符串是否以指定的前缀开头
区分大小写

boolean isEmpty()

判断字符串是否为空

int length()

获取字符串的长度

String substring(int start)

返回一个字符串,该字符串是此字符串的子字符串
子字符串以指定索引处的字符开头,并扩展到该字符串的末尾

String substring(int start,int end)

返回一个字符串,该字符串是此字符串的子字符串
子串开始于指定索引 start 并延伸到索引 end - 1
左闭右开 [ start,end )

猜你喜欢

转载自blog.csdn.net/qq_41464008/article/details/120660044