- String表示字符串,属于引用数据类型,不属于基本数据类型;
- 在Java中使用双引号括起来的都是String对象,是不可变的;
- 使用双引号括起来的字符串,都是直接存储在“方法区”的“字符串常量池”中,因为字符型在实际开发中使用太频繁,为了执行效率所以把字符串放到了字符串常量池当中;
//在字符串常量池中有三个字符串
String s1 = "abcd";
String s2 = "xyz" + "mn";
String s1 = "hello";
//"hello"是存储在方法区的字符串常量池当中
//所以这个"hello"不会新建。(因为这个对象已经存在了)
String s2 = "hello";
//双等号比较的是变量中保存的内存地址
System.out.println(s1 == s2)//true
String a = new String("xyz");
String b = new String("xyz");
//比较的是变量a和变量b的内存地址,因为new了两个对象,所以内存地址不相同
System.out.println(a == b)//false
//String类已经重写了equals方法,所以调用的是覆盖后equals方法
System.out.println(a.equals(b))//true
String k = null;
//这种存在空指针异常的风险
System.out.println(k.equals("xyz"));
//建议使用这种方式比较,可以避免空指针异常
System.out.println("xyz".equals(k));
//一共创建了几个对象?
String s1 = new String("hello");
String s2 = new String("hello");
//一共创建了3个对象
//方法区常量池中有一个"hello"
//堆内存中有两个String对象
关于String类中的构造方法
String x = new String("");
String y = "";
byte[] bytes = {97,98,99};//a,b,c
String s = new String(bytes);
System.out.println(s);//输出abc
//String类重写了toString()方法
//String(字节数组,元素起始下标,长度)
String s1 = new String(bytes,1,2);
System.out.println(s1);//bc
//将char数组全部转成字符串
char[] chars = {'我','是','中','国','人'}
String s2 = new String(chars);
System.out.println(s2);//我是中国人
//将char数组部分转成字符串
String s3 = new String(chars,2,3);
System.out.println(s3);//中国人
String类当中常用方法
//char charAt(index)
char c = "中国人".charAt(1);
System.out.println(c);//国
//int compareTo(String anotherString)
int i = "abc".compareTo("abc");
System.out.println(i);//0
int j = "abcd".compareTo("abce");
System.out.println(j);//-1 前小后大
int x = "abce".compareTo("abcd");
System.out.println(x);//1 前大后小
int y = "xyz".compareTo("yxz");
//拿着字符串第一个字母和后面字符串第一个字母比较
System.out.println(y);//-1
//boolean contains(CharSequence s)
//判断前面字符串包不包含后面字符串
System.out.println("hello world".contains("hello"));//true
//boolean endsWith(String suffix)
//判断当前字符串是否以某个字符串结尾
System.out.println("23435@qq.com".endsWith("@qq.com"));//true
//boolean equals(Object anObject)
System.out.println("abc".equals("abc"));//true
//boolean equalsIgnoreCase(String anotherString)
//判断两个字符串是否相等,并且忽略大小写
System.out.println("ABcd".equalsIgnoreCase("abCD"));//true
//byte[] getBytes()
//将字符串对象转为数组
byte[] bytes = "abcd".getBytes(StandardCharsets.UTF_8);
for (int k = 0; k < bytes.length; k++) {
System.out.println(bytes[k]);//97,98,99,100
}
//int indexOf(String str)
//判断某个子字符串在当前字符串中第一次出现处的索引(下标)
System.out.println("abcdcc".indexOf("c"));//2
//boolean isEmpty()
//判断某个字符串是否为空,底层调用的是length()方法
System.out.println("abc".isEmpty());//false
System.out.println("".isEmpty());//true
//int lastIndexOf(String str)
//判断某个字符串在当前字符串中最后异常出现的索引(下标)
System.out.println("hoool".lastIndexOf("oo"));//2
//String replace(CharSequence target,CharSequence replacement)
System.out.println("abc".replace("abc", "cdef"));//cdef
//String[] spilt(String regex)
//拆分字符串
String[] ymd = "2000-12-13".split("-");
for (int k = 0; k < ymd.length; k++) {
System.out.println(ymd[k]);//2000 12 13
}
//boolean startWith(String cprefix)
//判断当前字符串是否以某个字符串开始
System.out.println("http://1233".startsWith("http"));//true
//String substring(int beginIndex)起始下标
//截取字符串
System.out.println("HelloWorld".substring(2));//lloWorld
//String substring(int beginIndex,ind endIndex)起始下标,截止下标
//起始下标包括,截止下标不包括
System.out.println("HelloWorld".substring(2, 6));//lloW
//char[] toCharArray()
//将字符串转换成char数组
char[] chars = "我是中国人".toCharArray();
for (int k = 0; k < chars.length; k++) {
System.out.println(chars[k]);//我 是 中 国 人
}
//String toLowerCase()
//转换为小写
System.out.println("ABCD".toLowerCase(Locale.ROOT));//abcd
//String toUpperCase()
//转换为大写
System.out.println("abcd".toUpperCase(Locale.ROOT));//ABCD
//String trim()
//取出字符串前后空白
System.out.println(" abc ".trim());//abc
//String中只有一个方法是静态的,不需要new对象
//ValurOf 将非字符串转换成字符串
System.out.println(String.valueOf(true));//true