String class (Java)

1 Introduction

 A. Class introduction:
  Java treats strings as objects (different from C language, which directly uses character arrays to represent strings), and provides two String and StringBuffer Two classes are used to process immutable strings and variable strings respectively.
  The String class is mainly used for operations such as retrieval and comparison of string contents.

2. Analysis

 A. Class package structure: java.lang package – String class
Insert image description here
 B. Key core: String constants are also stored in the form of objects in Java
 C. Key core: String objects encapsulate data that is immutable. Changing the value of a reference variable actually points it to a new string object

3. Method

3.1 String() method

public class Main {
    
    
	public static void main(String[] args) {
    
    
		/* 
		 * public String() : 空字符串
		 * public String(String s) : 已有字符串赋值
		 * public String(StringBuffer s) : 使用StringBuffer 对象初始化
		 * public String(char value[]) : 使用字符数组赋值
		 */
		
		String s1 = new String();
		String s2 = new String("This is s1");
		String s3 = new String(new StringBuffer("This is s3"));
		String s4 = new String(new char[] {
    
    's', '4'});
	}	
}

3.2 equal() method

Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		String s1 = "abc";
		String s2 = "Abc";
		System.out.println(s1.equals(s2)); // false
		System.out.println(s1.equalsIgnoreCase(s2)); // true
	}	
}


&emsp: Details: equals() compares whether the contents of two objects are the same, s1 == s2 compares whether the references of the objects are the same.

public class Main {
    
    
	public static void main(String[] args) {
    
    
		String s1 = "abc";
		String s2 = "abc"; // 默认优化,相同字符串常量存储一次
		String s3 = new String("abc"); // 建立一个新对象

		System.out.println(s1 == s2); // true
		System.out.println(s1 == s3); // false
	}
}

3.3 compareTo() method

Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		/*
			当前串大 : return > 0
			当前串一样大 : return = 0
			当前串小 : return < 0
		 */
		String s1 = "aBc";
		String s2 = "ABc";
		System.out.println(s1.compareTo(s2));// > 0
		System.out.println(s1.compareToIgnoreCase(s2)); // 0
	}
}

3.4 contains() method

Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		String s = "abcdefghijk";
		String ss[] = {
    
    "a", "bc", "ff", "ij", "ji"};
		for(String x : ss){
    
    
			if(s.contains(x)) System.out.println("字符串 s 当中含有子串: " + x);
			else System.out.println("字符串 s 当中不含有子串: " + x);
		}
		/*
		字符串 s 当中含有子串: a
		字符串 s 当中含有子串: bc
		字符串 s 当中不含有子串: ff
		字符串 s 当中含有子串: ij
		字符串 s 当中不含有子串: ji
		 */
	}
}

3.5 toCharArray() method

Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		String s = "abcde";
		char x[] = s.toCharArray();
		for(int i = 0; i < x.length; i ++ )
			System.out.print(x[i]); // abcde
	}
}

3.6 trim() method

Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		String s = "  ab cde ";
		s = s.trim();
		System.out.print(s); // [ab cde]
	}
}

3.7 valueOf() method

Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		int x1 = 123;
		double x2 = 12.3;
		boolean x3 = true;
		char[] x4 = {
    
    '1', '2', '3'};
		Main x5 = new Main();

		System.out.println(String.valueOf(x1)); // 123
		System.out.println(String.valueOf(x2)); // 12.3
		System.out.println(String.valueOf(x3)); // true
		System.out.println(String.valueOf(x4)); // 123
		System.out.println(String.valueOf(x5)); // com.Main@1b6d3586
	}
}

3.8 subString() method

  Returns a string within the range [beging_index, end_index). If there is no end_index, it will be processed to the end of the string by default.
Insert image description here

public class Main {
    
    
	public static void main(String[] args){
    
    
		String s = "0123456789";
		String s1 = s.substring(3);
		String s2 = s.substring(3, 7);
		System.out.println(s1); // 3456789
		System.out.println(s2); // 3456
	}
}

3.9 indexOf() method

  Returns the index of the first occurrence of the specified character or string starting from the specified index [fromIndex] (can be omitted)
  If the specified character or string does not exist, or in If the specified index does not exist or is illegal, -1 will be returned
Insert image description here

public class Main {
    
    
	public static void main(String[] args){
    
    
		String s = "0123456789";

		System.out.println(s.indexOf('1')); // 1
		System.out.println(s.indexOf('1', 2)); // -1
		System.out.println(s.indexOf('a')); // -1

		System.out.println(s.indexOf("23")); // 2
		System.out.println(s.indexOf("23", 100)); // -1
		System.out.println(s.indexOf("ab")); // -1
	}
}

3.10 lastIndexOf() method

  The method is similar to the above-mentioned Indexof. The difference is that these methods are reverse search methods that start from the end of the string.
  If it exists legally, the specified position index is returned. None Legally existing results return -1
Insert image description here

public class Main {
    
    
	public static void main(String[] args){
    
    
		String s = "abcdeabcde";

		System.out.println(s.lastIndexOf('d')); // 8
		System.out.println(s.lastIndexOf('d', 7)); // 3
		System.out.println(s.lastIndexOf('f')); // -1

		System.out.println(s.lastIndexOf("cd")); // 7
		System.out.println(s.lastIndexOf("cd", 6)); // 2
		System.out.println(s.lastIndexOf("cd", -10)); // -1
	}
}

3.11 spilt() method

  Split the string according to the “x” character A string of 0, if it is at the end, this string will not appear

Insert image description here

import java.util.Arrays;

public class Main{
    
    
    public static void main(String[] args) throws Exception {
    
    
        String s = "x12x34"; // 切割字符串位于最初位置, 导致最初位置会有长度为0字符串
        System.out.println(Arrays.toString(s.split("x"))); // [, 12, 34]

        s = "12x34x"; // 若切割字符串位于最后面则不会出现
        System.out.println(Arrays.toString(s.split("x"))); // [12, 34]

        s = "12xx34"; // 若切割字符串连续出现在中间,则也会产生长度为0的字符串
        System.out.println(Arrays.toString(s.split("x"))); // [12, , 34]
    }
}

3.12 getBytes() method

  This method can be used to encode String into a Byte sequence according to the desired encoding.
Insert image description here

public class Main {
    
    
	public static void main(String[] args) throws Exception {
    
    
		String s = "12汉字";

		byte b1[] = new byte[20];
		b1 = s.getBytes(); // IDEAl 默认编码为 utf-8
		byte b2[] = new byte[20];
		b2 = s.getBytes("GBK"); // ”gbk编码“

		System.out.println(Arrays.toString(b1)); // [49, 50, -26, -79, -119, -27, -83, -105]
		System.out.println(Arrays.toString(b2)); // [49, 50, -70, -70, -41, -42]
	}
}

3.13 charAt() method

  If you want to get a single character, you can of course use the tocharArray() method to convert it into a character array and then access it. However, if you want to access a character that already knows the index specified position, you can directly use charAt() to get the character.
Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		String s = "abcd";
		for(int i = 0; i < s.length(); i ++ )
			System.out.println(s.charAt(i)); // a b c d
	}
}

3.14 equalsIgnoreCase() method

  Compares two strings, ignoring case within the strings for comparison.
Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		String x = "abC123";
		String y = "ABC123";
		System.out.println(x.equalsIgnoreCase(y)); // true
	}
}

3.15 replace() method

  Replace the specified character/string with a new character/string
Insert image description here

public class Main {
    
    
	public static void main(String[] args) {
    
    
		String s = "12a45aa67a";
		s = s.replace("a","x");
		System.out.println(s); // 12x45xx67x
	}
}

Guess you like

Origin blog.csdn.net/weixin_51566349/article/details/131223738