String类
package day6;import java.util.Arrays;public class StringTest { public static void main(String[] args) { // == 比较的是两个数据的大小 //基本数据类型的存储区域在栈中,每一个数据都是相互独立存在的 int i = 5; int j = 5; System.out.println(i==j); //数组是引用数据类型, //数组占用两块空间:栈和堆 //栈中存放数组数据在堆中存在的首地址(数组名) //堆中存放的是数组数据 int arr1[] = new int[2]; arr1[0] = 1; arr1[1] = 2; int arr2[] = new int[2]; arr2[0] = 1; arr2[1] = 2; System.out.println(arr1==arr2); System.out.println(Arrays.equals(arr1, arr2)); //普通的声明字符串的方法,只会在字符串常量池里开辟空间; //并且在开辟之前,会检查字符串常量池里是否已经存在; //相同的数据,如果有,直接指向已经存在的数据,如果没有,会在字符串常量池中开辟一个新的空间; String s = "ccy"; String s2 = "ccy"; System.out.println(s==s2); //在堆里开辟一个空间,存放内容,然后检查字符串常量池里又没有,没有就在池里开辟一个空间存放jereh String s3 = new String("jereh"); String s4 = new String("jereh"); System.out.println(s3 == s4); String s6 = new String("jredu"); String s5 = "jredu"; System.out.println(s5 == s6); }}
==================================================================================================================
package day6;public class Demo03 { //.concat() 链接字符串 public static void main(String[] args) { String string = new String("nihao"); String name = new String("zhangsan"); String abc = string.concat(name); System.out.println(abc); }}
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
package day6;import org.omg.CORBA.INTERNAL;import day52.shuzu;public class Demo04 { public static void main(String[] args) { String a = new String("asdf.java"); //indexOf() 获取字符串中某个字符或字符串出现的位置 System.out.println(a.indexOf("a")); //lastIndesOf() 获取字符串中某个字符或字符串最后一次出现的位置 System.out.println(a.lastIndexOf("a")); //substring(index)从指定位置(包含)开始截取字符串,知道最后一个 String newA = a.substring(4); System.out.println(newA); int index = a.indexOf("."); String newA2 = a.substring(index+1); System.out.println(newA2); //从指定索引开始(包含),到指定索引结束(不包含) String newA3 = a.substring(2,4); System.out.println(newA3); //trim()去前后空格 String s2 = " 杰 瑞 教 育 "; String newS4 = s2.trim(); System.out.println(newS4); } }
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package day6;public class Demo06 { public static void main(String[] args) { //分隔 String words = "长亭外 古道边 芳草碧连天 晚风拂柳笛声残 夕阳山外山"; String[] printword = new String[100]; System.out.println("***原歌词格式***\n" + words); System.out.println("\n***拆分后歌词格式***"); printword = words.split(" "); for (int i = 0; i < printword.length; i++) { System.out.println(printword[i]); } }}
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
package day6;public class Demo07 { public static void main(String[] args) { //StringBuffer //append() 用来向stringBuffer追加内容的 StringBuffer sb = new StringBuffer("QWER"); int num = 110; StringBuffer sb1 = sb.append("qwerty"); System.out.println(sb1); StringBuffer sb2 = sb1.append("qwerty"); System.out.println(sb2); StringBuffer sb3 = sb2.append(num); System.out.println(sb3); //String ---> StringBuffer StringBuffer s = new StringBuffer("qwer"); //StringBuffer ---> String sb.toString(); }}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
package day6;import java.util.Scanner;public class Demo08 { public static void main(String[] args) { //StringBuffer insert(索引index,方法content)方法 Scanner scanner = new Scanner(System.in); System.out.println("qingshuru"); String string = scanner.next(); StringBuffer buffer = new StringBuffer(string); for (int i = buffer.length()-3; i > 0; i-=3) { buffer.insert(i, ","); } System.out.println(buffer); scanner.close(); }}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
package day6;import java.util.Arrays;public class Test { public static void main(String[] args) { String string = "爱你,爱我,爱她,爱中国!爱"; string = " "+string+" "; int length = string.split("爱").length-1; System.out.println(length); }}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
package day61;public class Test { //全局变量有初始值 //局部变量没有初始值 int i; //OR下面那个int i = 0; public void check(){ // int i =0; System.out.println(i); } public static void main(String[] args) { Test test = new Test(); test.check(); }}