String类的位置
java.lang.String
String类的理解
1、String表示字符串类型,属于引用数据类型,不属于基本数据类型。
2、在Java中随便用双引号括起来的都是String对象。
3、java中规定,双引号括起来的字符串是不可变的,可变的只是引用。
4、双引号括起来的字符串都是直接存储在方法区的字符串常量池【属于方法区内存】中。
5、创建String对象时,如果是以【String s = "abc"】这种方式创建的,则引用s直接指向方法区内存的字符串常量池中的字符串,如果是以【String s = new String("abc")】这种方式创建的,则引用s指向的是堆内存中的String对象的地址,然后该String对象中存储的数据才是字符串的地址。
6、垃圾回收器不会回收字符串常量池中的数据。
7、判断字符串是否相等的时候,不要使用“==”判断,使用equals()方法判断。
(1)原因:若以‘==’判断,以【String s1 = "abc"】【String s2 = "abc"】这种方式创建的结果为true,以【String s1 = new String("abc")】【String s1 = new String("abc")】这种方式创建的,则返回值为false。【具体原因可参考第5条】
8、String类已经重写了toString()和equals()方法
String类常用的构造方法
// 第一种
String s = new String("");
// 第二种(最常用)
String s = "";
// 第三种
String s = new String(char数组)
// 第四种
String s = new String(char数组,起始下标,长度)
// 第五种
String s = new String(byte数组);
// 第六种
String s = new String(byte数组,起始下标,长度)
String类常用的方法
1、charAt
/**
* char charAt(int i)
* 作用:获取字符串下标为i的字符
*/
char c = "学习Java".charAt(2);
2、compareTo
/**
* int compareTo(String s)
* 作用:按照字典的顺序比较两个字符串大小
* 返回值: 1:前大后小
* 0:前后一致
* -1:前小后大
*/
int i = "xyz".compareTo("xyy");
System.out.println(i);
i = "xyz".compareTo("xyz");
System.out.println(i);
i = "xyz".compareTo("xzz");
System.out.println(i);
3、contains
/**
* boolean contains(CharSequence s)
* 作用:判断前面的字符串是否包含后面的子字符串
*/
System.out.println("www.baidu.com".contains("com"));// true
System.out.println("www.baidu.com".contains("con"));// false
4、endsWith
/**
* boolean endsWith(String suffix)
* 作用:判断是否以某个字符串结尾
*/
System.out.println("www.baidu.com".endsWith("com"));// true
System.out.println("www.baidu.com".endsWith("con"));// false
5、equals
/**
* boolean equals(Object anObject)
* 作用:比较两个字符串是否相等【不能使用==】
*/
System.out.println("www.baidu.com".equals("www.baidu.com"));// true
System.out.println("www.baidu.com".equals("www"));// false
6、equalsIgnoreCase
/**
* boolean equalsIgnoreCase(String anotherString)
* 作用:判断两个字符串是否相等,并同时忽略大小写
*/
System.out.println("www.baidu.com".equalsIgnoreCase("WWW.BaIdU.COM"));// true
System.out.println("www.baidu.com".equalsIgnoreCase("WWW.BAIDU"));// false
7、getByte
/**
* byte[] getBytes()
* 作用:将字符串对象转换为字符串数组
* 返回值:字符串数组
*/
byte[] bytes = "abcde".getBytes();
for (int j = 0; j < bytes.length; j++) {
System.out.print(bytes[j] + "、");
}
//输出:97、98、99、100、101、
8、indexOf
/**
* int indexOf(String str)
* 作用:判断某个子字符串在当前字符串第一次出现的索引(下标)
*/
System.out.println("www.baidu.com".indexOf("baidu"));// 4
9、isEmpty
/**
* boolean isEmpty()
* 作用:判断某个字符串是否为空【注意不是判断是否为null,如果为null则空指针异常】
*/
System.out.println("".isEmpty());// true
System.out.println("1".isEmpty());// false
10、lastIndexOf
/**
* int lastIndexOf(String str)
* 作用:判断某个子字符串在当前字符串中最后一次出现的索引(下标)
*/
System.out.println("www.baidu.com".lastIndexOf("w"));// 2
11、replace
/**
* String replace(CharSequence target, CharSequence replacement)
* String的父接口就是CharSequence
* 作用:将指定字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
*/
System.out.println("www.baidu.com".replace("w", "c"));// ccc.baidu.com
12、split
/**
* String[] split(String regex)
* 作用:拆分字符串
*/
String[] ymd = "2022-3-6".split("-");
for (int j = 0; j < ymd.length; j++) {
System.out.print(ymd[j]);
}
// 输出: 202236
13、startsWith
/**
* boolean startsWith(String prefix)
* 作用:判断是否以某个字符串开始
*/
System.out.println("www.baidu.com".startsWith("www"));// true
System.out.println("www.baidu.com".startsWith("com"));// false
14、subString
/**
* String substring(int beginIndex)
* 作用:截取字符串,参数是起始下标(保留起始下标)
*/
System.out.println("Http://www.baidu.com".substring(7));// www.baidu.com
/**
* String substring(int beginIndex, int endIndex)
* 作用:截取字符串,参数是起始下标(保留起始下标,不保留终止下标)左闭右开
*/
System.out.println("Http://www.baidu.com".substring(7, 10));// www
15、toCharArray
/**
* char[] toCharArray()
* 作用:将字符串转化为字符数组
*/
char[] d = "测试一下".toCharArray();
for (int j = 0; j < d.length; j++) {
System.out.println(d[j]);
}
// 输出结果: 测
// 试
// 一
// 下
16、toLowerCase和toUpperCase
/**
* String toLowerCase()
* 作用:将字符串所有字符转换为小写
* String toUpperCase()
* 作用:将字符串所有字符转换为大写
*/
System.out.println("WWW.BAIDU.COM".toLowerCase());// www.baidu.com
System.out.println("www.baidu.com".toUpperCase());// WWW.BAIDU.COM
17、trim
/**
* String trim()
* 作用:去除字符串前后空白
*/
System.out.println(" www.bai du.com ".trim());// www.bai du.com
18、valueOf
/**
* String valueOf()
* 作用:String只有这一个静态方法,将非字符串转换成字符串
* 注:若参数是一个对象,则会自动调用该对象的toString()方法
*/
System.out.println(String.valueOf(true));// 字符串true