Detailed explanation of the use of the Java String class


1. Overview and characteristics of the String class

1 Overview

  1. The String class represents strings. All string literals (such as "abc") in Java programs are implemented as instances of this class.

2. Features

  1. Strings are constants, and their values ​​are in 创建之后不能更改.
  2. The string buffer supports variable strings. Because String 对象是不可变的,所以可以共享of
  3. 字符串效果上相当于char[]数组, But 底层原理是byte[]数组.

Second, the creation of strings and the concept of constant pools

1. Commonly used string creation form

Create directly using double quotes:

直接写上双引号,就是创建了String对象

String str = "abc";
System.out.println("直接使用双引号创建的字符串为:"+str);//结果为:abc

Four construction methods are created:

1.public String():创建一个空白字符串

//初始化一个新创建的 String 对象,使其表示一个空字符序列。
String str = new String();
System.out.println("使用public String()创建的字符串为:"+str);

2 public String(String str).: Use有参方法创建字符串

//初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。
String str = new String("abc");
System.out.println("使用public String(String str)创建的字符串为:"+str);//结果为:abc

3 public String(char[] array).: According to the 字符数组的内容创建corresponding string

//分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
char[] charArray = {
    
    'a','b','c'};
String str = new String(charArray);
System.out.println("使用public String(char[] array)创建的字符串为:"+str);//结果为:abc

4 public String(byte[] array).: According to the 字节数组的内容创建corresponding string

//通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
byte[] byteArray = {
    
    97,98,99};
String str = new String(byteArray);
System.out.println("使用public String(byte[] array)创建的字符串为:"+str);//结果为:abc

In the ASCII code table, 65 represents the letter A, 97 represents the letter a
To learn more about the ASCII code table, click here

2. Constant pool

String constant pool (in heap memory)

  • The directly used 双引号定义string is 常量池in
  • Use the construction method newto open up another memory space in the middle
    . The
    memory diagram is as follows
    Insert picture description here

Third, the commonly used methods of the String class

For the basic data type == is the comparison of the value.
For the reference data type == is the comparison of the address value.

1. Related methods of string comparison

(1) The equals method , which compares the content of the string

Information method:
public boolean the equals (Object obj)
parameter can be any object, and only 参数是一个字符串并且内容相同will返回true

//定义三个字符串
String str1 = "Hello";
String str2 = new String("Hello");
String str3 = "hello";

//比较定义方式不同,但是内容相同的字符串
System.out.println(str1.equals(str2));//结果为:true

//比较定义方式相同,但是内容不同的字符串
System.out.println(str1.equals(str3));//结果为:false

//使用 "" 定义字符串,直接.equals方法
System.out.println("Hello".equals(str1));//结果为:true

If you use equals() to compare a constant to a variable,通常常量在前,变量在后

String str4 = null;
//System.out.println(str4.equals(""));
//不推荐,如果变量为空,会报java.lang.NullPointerException错误
System.out.println("".equals(str4));//结果为:false

(2) equalsIgnoreCase method , 忽略英文大小写content comparison

Method information:
public boolean equalsIgnoreCase()

String str5 = "Java";
String str6 = "java";
System.out.println(str5.equals(str6));//结果为:false
System.out.println(str5.equalsIgnoreCase(str6));//结果为:true

2. Related methods for obtaining strings

(1) length method ,获取字符串长度

Method information:
public int length()

int l = "qwertyuiopasdfghjkl".length();
System.out.println("字符串长度为:"+l);
//结果为:字符串长度为:19

(2) The concat method will参数字符串与原字符串进行拼接

Method information:
public String concat(String str)

String str1 = "Hello";
String str2 = "World";
System.out.println(str1.concat(str2));
//结果为:HelloWorld

(3) The charAt method , get指定索引位置的单个字符

Method information:
public char charAt(int index)

char ch = "Hello".charAt(1);
System.out.println("1号索引位置的元素为:"+ch);
//结果为:1号索引位置的元素为:e

(4) indexOf method , find 参数字符串在本字符串当中首次出现的索引位置if没有返回-1

Method information:
public int indexOf(String str)

String str3 = "HelloWorld";
String str4 = "ll";
String str5 = "abc";
System.out.println(str4+"第一次在"+str3+"中出现的索引位置为:"+str3.indexOf(str4));
//结果为:ll第一次在HelloWorld中出现的索引位置为:2
System.out.println(str5+"第一次在"+str3+"中出现的索引位置为:"+str3.indexOf(str5));
//结果为:abc第一次在HelloWorld中出现的索引位置为:-1

3. Related methods of string interception

(1) Substring method (a parameter) , intercepted from参数索引开始一直到字符串的最后
The nrelationship between the first element and the index isn+1

Method information:
public String substring(int index)

String str1 = "HelloWorld";
String str2 = str1.substring(5);
System.out.println(str2);//结果为:World

(2) The substring method (two parameters) , intercept [begin,end)the string at the index position

Method information:
public String substring(int begin ,int end)

String str1 = "HelloWorld";
String str3 = str1.substring(2,5);
System.out.println(str3);//结果为:llo

4. Related methods of string conversion

(1) toCharArray method , the string拆分为字符数组

Method information:
public char[] toCharArray()

//将字符串拆分为字符数组
String str1 = "HelloWorld";
char[] chArr = str1.toCharArray();

(2) getBytes method , the current string转换为底层的字符数组

Method information:
public byte[] getBytes()

//将字符串拆分为字节数组
String str1 = "HelloWorld";
byte[] byArr = str1.getBytes();

(3) The replace method , the string某一字符全部替换为新字符

Method information:
public String replace(CharSequence oldString ,CharSequence newString) The
first parameter is the old character, and the second parameter is the new character

//将 "l" 替换为 "*"
String str1 = "HelloWorld";
String str2 = str1.replace("l", "*");
System.out.println(str1);//HelloWorld
System.out.println(str2);//He**oWor*d

5. Related methods of string segmentation

split method ,按照参数的规则,分割字符串

Method information:
public String[] split(String regex) Click here to
参数regex actually 为正则表达式
understand regular expressions

String str1 = "aaa,bbb,ccc";
String[] stringArray = str1.split(",");//参数为正则表达式
for (int i = 0; i < stringArray.length; i++) {
    
    
      System.out.println(stringArray[i]);
}
//结果为:
//aaa
//bbb
//ccc

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/106026843