String类表示字符串,是由多个字符组成的一串数据(字符序列),java中所有的字符串都是String类的实例
如:
String s = "abc";
//其底层为一个char数组 char[] c = {'a','b','c'}
-
在java程序中所有的字符串常量,如"abc",都被实现为String类的实例
-
String类型的数据其值一旦创建不可修改,因为其底层代码为
private final char value[]; //被final修饰,不可更改2.String对象的创建方法 ①String s = "abc"; ②String s1 = new String();
-
方式①: 创建时先去字符串常量池中查找有没有相应的字符(abc),如果没有,就在字符串常量池中创建一个相应的对象(abc);如果有,则直接指向已有对象即可
-
方式②: 创建一个新的独立对象,在内存空间中独一无二
String s = "abc";
String s1 = "abc";
System.out.println(s==s1); //字符串常量池中查找,s已创建,所以结果为true
String s2 = new String("abc");
String s3 = new String("abc");
System.out.println(s2==s3); //两个不同的对象,结果为false
3.String类的构造方法
①无参构造方法
String();
public String(){
this.value = " ".value;
}
②有参构造方法
String("字符串");
String s1 = new String("abc");
4.String类常用方法举例
①判断功能
String s1 = "abc";
String s2 = "abc";
System.out.println(s1.equals(s2)); //比较内容是否相等,所以结果为true
System.out.println(s1.equalsIgnoreCase(s2)); //比较内容是否相等(忽略大小写),结果为true
System.out.println(s1.contains("a")); //判断是否包含指定字符,结果为true
System.out.println(s1.isEmpty()); //判断字符串是否为空,结果为false
System.out.println(s1.startsWith("a")); //判断是否以指定字符开头,结果为true
System.out.println(s1.endsWith("bc")); //判断是否以指定字符结尾,结果为true
System.out.println(s1.compareTo(s2)); //字符串比较大小,结果为 0
②获取功能
String s9 = "abcabcabce";
String s8 = "defg";
System.out.println(s9.length()); //获取字符串的长度,结果为10
char c = s9.charAt(3); //获取指定位置(索引)上的字符,结果为a
System.out.println(c);
int index1 = s9.indexOf("e"); //从前向后查找指定字符,只找首次出现的位置,结果为9
System.out.println(index1);
int index2 = s9.indexOf("c",index1+1); //从前向后找,从指定位置开始,因为index+1为10,所以结果为-1
System.out.println(index2);
System.out.println("*********");
System.out.println(s8.lastIndexOf("e")); //从后向前找,结果为1
String s7 = s9.substring(3); //从指定位置开始截取字符串,直至最后一位,结果返回一个新的字符串对象
System.out.println(s7); //结果为abcabce
String s6 = s9.substring(2,6); //截取某个指定区间,包含开始,不包含结尾
System.out.println(s6); //结果为cabc
③转换功能
String s = "你好"; //默认是utf-8编码,在utf-8中,一个汉字占三个字节
byte [] b = s.getBytes("UTF-8"); //编码:将字符串转为指定编码格式
System.out.println(b); //这里输出的是"你好"这个字符串转为UTF-8后的代码
String s1 = new String(b,"UTF-8"); //解码
System.out.println(s1);
char [] chars = s.toCharArray(); //将字符串转为一个char类型的数组
System.out.println(chars);
String s2 = new String(chars); //将一个char类型的数组转换成一个字符串类型
System.out.println(s2);
String s3 = "sbcSDFH";
String s4 = s3.toUpperCase(); //将字符串中的字母转换成大写字母
String s5 = s3.toLowerCase(); //将字符串中的字母转换成小写字母
System.out.println(s4); //结果为SBCSDFH
System.out.println(s5); //结果为sbcsdfh
String s6 = s3.concat("你好"); //将字符串"你好",连接在s3这个字符串上
System.out.println(s6); //结果为sbcSDFH你好
String s7 = "shkj:gi:sbf";
String [] strings = s7.split(":"); //按照指定分隔符,将字符串拆分为数组
//regex:正则表达式,在我的博客中有单独介绍
System.out.println(Arrays.toString(strings)); //结果为[shkj, gi, sbf]
④替换功能
String s = " a vs6oa1h ";
System.out.println(s);
System.out.println(s.replace("a","A")); //替换字符串中一个或连续字符
System.out.println(s.replaceAll("\d","D")); //第一个参数为正则表达式,是一种匹配模式
System.out.println(s.replaceFirst("\d","F")); //第一个参数也为正贼表达式,但是只替换第一个
⑤去除字符串两端空格
System.out.println(s.trim()); //去除字符串两端空格,结果为a vs6oa1h



