字符串的构造方法和直接创建
package com.Demo04;/*字符串得特带你:1.字符串得内容永远比可变 【重点】2.正式因为字符串不可改变,所以字符串是可以共享使用的3.字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组创建字符串的常见3+1种方式三种构造方法:public String():创建一个空白字符串,不含有任何内容public String(char[] array):根据字符数组的内容,来创建对应的字符串public String(byte[] array):根据字节数组的内容,来创建对应的字符串一种直接创建:String str="Hello"; //右边直接用双引号注意:直接写上双引号,就是字符串对象*/public class Demo01String {public static void main(String[] args) {//空参构造String str = new String();System.out.println("空参构造字符串是:"+str); ////根据字符数组创建字符串char[] charArray={'A','B'};String str1=new String(charArray);System.out.println("根据字符数组创建字符串:"+str1); //AB//根据字节数组创建字符串byte[] byteArray=new byte[]{97,98,99};String str2=new String(byteArray);System.out.println("根据字节数组创建字符串:"+str2);//直接创建String str3="Hello";System.out.println("直接创建的字符串:"+str3);}}
字符串的常量池
package com.Demo04;/*字符串常量值:程序当中直接写上双引号的字符串,在字符串常量池中对于基本类型来说:==是进行数值的比较对于引用类型来说:==是进行地址值的比较*/public class Demo02String {public static void main(String[] args) {String str1="abc";String str2="abc";char[] charArray = new char[]{'a','b','c'};String str3=new String(charArray);System.out.println(str1==str2); //trueSystem.out.println(str2==str3); //falseSystem.out.println(str1==str3); //false}}
字符串的比较相关方法
package com.Demo04;/*==是对象进行的一个地址值比较,如果确实需要字符串的内容比较,可以使用两个方法:*public boolean equals(Object obj):参数可以是任何对象,只有蚕食是一个字符串并且内容相同的才会返回true,否则返回false注意事项:1.所有对象都能用Object进行接收2.equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样3.如果比较双方一个变量一个常量,推荐将常量字符写在前面推荐 "hello".equals(str1) 不推荐str1.equals("hello")*public boolean equalsIgnoreCase():忽略大小写,进行内容的比较只有英文区分大小写*/public class Demo03String {public static void main(String[] args) {String str1="Hello";String str2="Hello";char[] charArray=new char[]{'H','e','l','l','o'};String str3=new String(charArray);System.out.println(str1.equals(str2)); //trueSystem.out.println(str1.equals(str3)); //trueSystem.out.println(str2.equals(str3)); //trueSystem.out.println("Hello".equals(str3)); //trueSystem.out.println(str3.equals("Hello"));//trueSystem.out.println("==========================");String str4 = null;System.out.println("Hello".equals(str4)); //推荐// System.out.println(str4.equals("Hello")); //不推荐 报错 空指针异常NullPointerExceptionSystem.out.println("===============================");System.out.println("hello".equalsIgnoreCase(str1)); //true}}
字符串的获取相关方法
package com.Demo04;/*String当中与获取相关的常用方法有:public int length():获取字符串当中还有的字符个数,拿到字符长度Public String concat(String str):将当前字符串和参数字符串拼接成为返回值 新的字符串public char charAt(int index):获取指定索引位置的单个字符public int indexOf(String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1*/public class Demo04String {public static void main(String[] args) {//获取字符串长度String str1="hsidjfhshfkl";int length=str1.length();System.out.println("字符串长度:"+length);//拼接字符串String str2="Hello";String str3=" Java";String newStr=str2.concat(str3);System.out.println(newStr);//获取指定索引位置的单个字符String str4="kekeaiai";System.out.println(str4.charAt(4));//查找参数字符串在本来字符串当中第一次出现的索引值String str5="kekedddddaiai";int index=str5.indexOf("ai");System.out.println("ai第一次出现的位置:"+index);System.out.println(str5.indexOf("11111")); //-1}}
字符串的截取方法
package com.Demo04;/*字符串的截取方法:public String subString(int index):截取从索引位置开始一直到字符串末尾public String subString(int begin,int end):截取从begin开始,一直到end结束,中间的字符串。备注:[begin,end),包含左边不包含右边*/public class Demo05String {public static void main(String[] args) {String str1="HelloWorld";String str2=str1.substring(5);System.out.println(str1); //HelloWorld,原封不动System.out.println(str2); //World,新字符串System.out.println("====================");String str3 = str1.substring(4,7); //owoSystem.out.println(str3);System.out.println("====================");//下面这种写法,字符串的内容仍然时没有改变的//下面又两个字符串“Hello”,“Java”//strA当中保存的是地址值//本来地址值是Hello的0x666//后来地址值变成了Java的0x999String strA = "Hello";System.out.println(strA); //HellostrA = "Java";System.out.println(strA); //Java}}
字符串的转换相关方法
package com.Demo04;/*String当中与转换相关的常用方法有:public char[] toCharArray():将当前字符串拆分成为字符数组作为返回值public byte[] getBytes():获取当前字符串底层的字节数组public String replace(CharSequence oldString,CharSequence newString):将所有出现的老子非常替换成为新的字符串,返回替换之后的结果新字符串*/public class Demo06StringTurn {public static void main(String[] args) {//转换成为字符数组char[] chars= "Hello".toCharArray();System.out.println(chars[0]); //HSystem.out.println(chars.length); //5System.out.println("=======================");//转换成为字节数组byte[] bytes= "abc".getBytes();System.out.println(bytes); //地址值for (int i = 0; i < bytes.length; i++) {byte aByte = bytes[i];System.out.println(aByte);}System.out.println("=======================");//字符串的内容替换String str1="How do you do?";String str2=str1.replace("do","*");System.out.println(str2); //How * you *?}}
字符串的分割方法
package com.Demo04;/*分割字符串的方法:public String[] split(String regex):根据参数的规则,将字符串气氛成为若干部分注意事项:split方法的参数其实是一个“正则表达式”,----今后学习今天要注意:如果按照英文句点“.”进行分割,必须写“\\.”,两个反斜杠*/public class Demo07StringSpite {public static void main(String[] args) {String str2 ="aaa,bbb,ccc";String[] arr1=str2.split(",");System.out.println(arr1);for (int i = 0; i < arr1.length; i++) {String s = arr1[i];System.out.println(s);}System.out.println("===================");String str1="qqq.wwww.rrrr";String[] arr2=str1.split("\\.");for (int i = 0; i < arr2.length; i++) {String s = arr2[i];System.out.println(s);}}}
练习
题目一:按指定格式拼接字符串{1,2,3}—->[word1#word2#word3]
package com.Demo04;public class Demo08StringWork {public static void main(String[] args) {int[] array={1,2,3};String result=arrayToString(array);System.out.println(result);}public static String arrayToString(int[] array){String str="[";for (int i = 0; i < array.length; i++) {if(i== array.length-1){str=str+"word"+array[i]+"]";}else {str = str +"word"+array[i]+ "#";}}return str;}}
题目二:统计输入的字符串中大小写、数字、和其他字符的个数
package com.Demo04;import java.util.Scanner;//题目二:统计输入的字符串中大小写、数字、和其他字符的个数public class Demo08StringWork01 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入需要统计的字符串:");String str=sc.next();arrays(str);}public static void arrays(String str){int lowerChar=0;int upperChar=0;int numberChar=0;int otherChar=0;char[] arr=str.toCharArray();for (int i = 0; i < arr.length; i++) {char c = arr[i];if ('a'<=c && c<='z'){lowerChar++;}else if('A'<=c && c<='Z'){upperChar++;}else if('0'<=c && c<='9'){numberChar++;}else{otherChar++;}}System.out.println("小写字母有:"+lowerChar+"个");System.out.println("大写字母有:"+upperChar+"个");System.out.println("数字有:"+numberChar+"个");System.out.println("其他字符有:"+otherChar+"个");}}
