Java entry learning log six (object-oriented 4)

Basic concepts of String

1: You can use "==" to compare the reference types, but the comparison is not the content, but the numerical content of the address. Such operations often occur when determining whether two objects with different names point to the same memory space.
2: The string constant is an anonymous object of String.
3: String instantiation is divided into direct assignment instantiation and constructor instantiation.
4: Using the direct assignment method to instantiate the String class will only open up a heap of memory. And the anonymous object used by this string object will be pooled and saved. If there are other String classes behind which also use direct assignment and set the same content, it will not open up new memory, but use existing objects for reference.
5: Use the constructor to instantiate the String class object, which will open up two heap memories. Since new always means to open up a new heap memory space, it will never be saved in the object pool.
6: Once the character string is defined, it cannot be changed.

String common methods

public class StringDemo{
	public static void main(String args[]) {
		String str ="hello fool" ;
		String temp=new String("hello fool");//new开辟了新的内存
		System.out.println(str==temp);  //比较的是两个字符串的内存地址
		System.out.println(str.contentEquals(temp));//比较两个字符串的内容
		char ch = str.charAt(6);          // charAt(int index) 取出字符串中指定索引的字符
		System.out.println(ch);          //输出 f
		char[] ch1 = str.toCharArray(); //toCharArray()将字符串转变为字符数组
		for(int i=0;i<ch1.length;i++) { //拆分后的字符数组长度就是字符串长度
			System.out.print(ch1[i]+" ");//输出h e l l o   f o o l
		}
		for(int j=0;j<ch1.length;j++) {
			ch1[j] -=32;  // 将小写字母变为大写字母
		}
		System.out.println();
		System.out.println(new String (ch1)); //利用构造方法string(char [])将字符型数组全部变为字符串
		System.out.println(new String(ch1,6,4));//构造方法string(char [],int ,int);指定字符型数组 ,索引,长度。
		String str1 ="HEllo fool";
		System.out.println(str.contentEquals(str1)); // 区分大小写的比较两个字符串
		System.out.println(str.equalsIgnoreCase(str1));//不区分大小写的比较两个字符串
		System.out.println(str1.compareTo(str));  //两个字符串相等则输出0 ,str1大于str则输出大于零的值 ,否则返回小于0的值
		System.out.println(str.indexOf("fool")); //从前向后寻找满足条件单词的第一个索引
		System.out.println(str.indexOf("l"));   
		System.out.println(str.indexOf("l",5)); //从第六个元素开始查找指定字符串的位置
		System.out.println(str.lastIndexOf("l"));//从后行前寻找指定字符串的位置
		System.out.println(str.indexOf("foof")); //如果寻找不到指定字符串 输出-1
		System.out.println(str.contains("fool"));//是否存在指定字符串 返回布尔值
		System.out.println(str.startsWith("hello"));//是否以"hello"开头
		System.out.println(str.startsWith("f",6));//从第6个索引开始是否以"f"开头
		System.out.println(str.endsWith("ol"));  //是否以"ol"结尾
		System.out.println(str.replaceAll("l","--"));//将字符串中的"l"全部替换为"--";
		System.out.println(str.replaceFirst("l","="));//将字符串中的第一个"l"替换为"=";
		System.out.println(str);  //验证 :字符串一旦定义则不可被更改
		System.out.println(str.substring(6)); //从指定索引开始 ,截取后面所有的字符串
		System.out.println(str.substring(1,3));//从指定索引开始,截取指定到指定索引以前的字符串
		String str2[]=str.split(" ");  //以" "进行拆分 返回字符串数组
		for(int k=0;k<str2.length;k++) {
			System.out.println(str2[k]);
		}
		String str3[]=str.split("");  //可以发现 ,使用split方法时只设置了一个空字符 ,则表示对每个字符都进行拆分
		for(int p=0;p<str3.length;p++) {
			System.out.println(str3[p]);
		}
		String str4[]=str.split("",2);  //截取指定数量的字符串
		for(int q=0;q<str4.length;q++) {
			System.out.println(str4[q]);
		}
		System.out.println(str.toUpperCase());//字符串转大写
		System.out.println(str1.toLowerCase());//字符串转小写
		System.out.println(str.length()); //返回字符串的长度 这里需要注意与数组的length区别开 ,此处是String中的方法加上() ;
		System.out.println(str.isEmpty());//判断指定字符串是否为空字符 返回布尔值
		System.out.println("".isEmpty());
	}
}
Published 42 original articles · Like 15 · Visitors 6725

Guess you like

Origin blog.csdn.net/guyjy/article/details/105567398