💨💨💨——String字符串的简介和存储原理
- String字符串的简介
关于java JDK中内置的一个类:java.lang.String
- String表示字符串类型,属于引用数据类型,不属于基本数据类型
- 在java中随便使用双引号括起来的都是String对象,例:“abc”、“hello world!”
- java中规定,在JDK当中双引号括起来的字符串,是不可变的(都是储存在方法区的字符串常量池当中的)
——为什么SUN公司把字符串存储在一个==字符串常量池==当中呢?
——因为字符串在实际开发中使用太频繁,为了执行效率,所以把字符串放在方法区的字符串常量池当中
- String字符串的储存原理
public class StringTest01{ public static void main(String[] args){ // 这两行代码表示的底层创建了3个字符串对象,都在字符串常量池当中 String s1="abcdef"; String s2="abcdef"+"xy"; //分析:这是使用new方式创建的字符串对象,这个代码中的"xy"是从哪里来的 //解析:凡是双引号括起来的都在字符串常量池中有一份,new对象时一定在堆内存中开辟空间 String s3=new String("xy"); } }
💨💨💨——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类的方法
- 常用的方法
- charAt(int index):获得该字符串相应索引位置的值
———— 返回值为char
—— 返回指定索引处的值System.out.println("erdong".charAt(2));//d
- compareTo(String anotherString):按字典顺序比较两个字符串中相对应索引位置每个字符的Unicode值,不为0则返回输出比较值后停止后续比较
———— 返回值为int
—— 返回值为0,表示当前字符串与参数字符串相等
—— 返回值为正数,表示当前字符串按字典顺序排列在参数字符串之后
—— 返回值为负数,表示当前字符串按字典顺序排列在参数字符串之前
注:System.out.println("123cdswwq".compareTo("123cdswwq"));//0 System.out.println("523ddswwq".compareTo("123cdswwq"));//4 System.out.println("023fdswwq".compareTo("123cdswwq"));//-1
equals方法老版本调用了compareTo方法;新版本没有直接调用
equals方法只能看出相不相等;compareTo方法可以看出是否相等,并且同时还能看出谁大谁小
- equals(Object anObject):判断当前字符串是否与参数字符串相等
———— 返回值为boolean
注:System.out.println("abcd".equals("abcd"));//true
equals方法老版本调用了compareTo方法;新版本没有直接调用
equals方法只能看出相不相等;compareTo方法可以看出是否相等,并且同时还能看出谁大谁小
- equalsIgnoreCase(String anotherString):忽略大小写判断两个字符串是否相等。
———— 返回值为booleanSystem.out.println("a123BcaD".equalsIgnoreCase("A123bCad"));//true
- contains(CharSequence s):判断当前字符串中是否包含参数字符串。
———— 返回值为booleanSystem.out.println("HelloWorld.java".contains("World"));//true System.out.println("http://www.baidu.com".contains("https"));//false
- startsWith(String prefix):判断当前字符串是否以参数字符串开始。
———— 返回值为booleanSystem.out.println("https://www.baidu.com".startsWith("https"));//true System.out.println("http://www.baidu.com".startsWith("https"));//false
- endsWith(String suffix):判断当前字符串是否以参数字符串结尾。
———— 返回值为booleanSystem.out.println("HelloWorld.java".endsWith(".java"));//true System.out.println("HelloWorld.java".endsWith(".text"));//false
- getBytes():将字符串对象转换成字节数组
———— 返回值为byte[]byte[] bytes = "a1b2c3d4".getBytes(); System.out.println(Arrays.toString(bytes));//[97, 49, 98, 50, 99, 51, 100, 52]
- toCharArray():将字符串转换成char数组
———— 返回值为char[]char[] chars = "a1b2c3d4".toCharArray(); System.out.println(Arrays.toString(chars));//[a, 1, b, 2, c, 3, d, 4]
- indexOf(String str):判断参数字符串在当前字符串中第一次出现处的索引(下标)。
———— 返回值为intSystem.out.println("pythonC++CC#javaPHP".indexOf("java"));//12
- lastIndexOf(String str):判断参数字符串在当前字符串中最后一次出现的索引(下标)
———— 返回值为intSystem.out.println("pythonC++CC#javaPHP".lastIndexOf("C"));//10
- isEmpty():判断当前字符串是否为“空字符串”。
———— 返回值为boolean
注:底层源代码调用的应该是字符串的length()方法。
System.out.println("11wwe".isEmpty());//false System.out.println(" ".isEmpty());//false System.out.println("".isEmpty());//true
- length():获得字符串长度(字符个数)
———— 返回值为int
注:判断数组长度是length属性;判断字符串长度是length()方法
System.out.println("length()".length());//8
- replace(CharSequence target, CharSequence replacement):将当前字符串的某段字符串替换为参数字符串。
———— 返回值为String
注:String的父接口就是 CharSequence
System.out.println("http://www.baidu.com".replace("http://", "https://"));//https://www.baidu.com
- split(String regex):拆分当前字符串
———— 返回值为String[]String[] s = "name=zhangsan&password=1234&age=23".split("&"); System.out.println(Arrays.toString(s));//[name=zhangsan, password=1234, age=23] for (int i = 0; i < s.length; i++) { String[] s1 = s[i].split("="); for (int j = 0; j < s1.length; j++) { System.out.print(s1[j]+"\t");//name zhangsan password 1234 age 23 } System.out.println(); }
- substring(int beginIndex):截取起始索引(包含)之后的字符串
———— 返回值为StringSystem.out.println("https://www.baidu.com".substring(8));//www.baidu.com
- substring(int beginIndex, int endIndex):截取起始索引(包含)到结束索引(不包含)之间的字符串
———— 返回值为StringSystem.out.println("https://www.baidu.com".substring(8,11));//www
- toLowerCase():将当前字符串全部转换为小写。
———— 返回值为StringSystem.out.println("a1B2C3d4E".toLowerCase());//a1b2c3d4e
- toUpperCase():将当前字符串全部转换为大写。
———— 返回值为StringSystem.out.println("a1B2C3d4E".toUpperCase());//A1B2C3D4E
- trim():去除当前字符串前后空白
———— 返回值为StringSystem.out.println(" hello world ".trim());//hello world
- valueOf(Object obj):将“非字符串”转换成“字符串”
———— String中唯一一个静态方法,不需要new对象
注:System.out.println(String.valueOf(true));//true System.out.println(String.valueOf(100));//100 System.out.println(String.valueOf(new Object()));//java.lang.Object@1b6d3586 System.out.println(String.valueOf(new String("hello")));//hello
当参数是一个对象的时候,会自动调用该对象的toString()方法
为什么输出一个引用的时候,会调用toString()方法!!!!
————本质上System.out.println()这个方法在输出任何数据的时候都是先转换成字符串,再输出。
- charAt(int index):获得该字符串相应索引位置的值