Detailed explanation of String in java (creating strings, comparing strings, finding strings, conversion between String and int, string case conversion, string to array)


foreword

This article mainly introduces the String string class in java in detail, including creating strings, comparing strings, searching strings, and converting strings.


1. String class

In java, the String class represents a string and belongs to an object.

1. Create a string

(1) Create directly

String s = "hello";

(2) Use the constructor to create a String object

The following are examples of different construction methods:

//1.String(String Original),把字符串封装成字符串对象
String s1 = new String("hello world");

//2.String(char[] value),把字符数组封装成字符串对象
char[] c1 = {
    
    'h','a','h','a','!'};   
String s2 = new String(c1);

//String(char[] value,int index, int count)
//将index到count个字符数组值封装成字符串对象
char[] c2 = {
    
    'h','a','h','a','!'};   
String s3 = new String(c2,1,3);

2. The difference between the above two ways to create

(1) Created by direct assignment: the object is a string constant in the constant pool in the method area.
(2) Created by the construction method: the String object is stored in the heap memory of the java virtual machine. That is: the address of the string constant is stored in the heap memory, and the string constant is stored in the constant pool in the method area.
example:

public static void main(String[] args) {
    
    
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = "hello";
        String s4 = "hello";
        System.out.println(s1==s2);
        System.out.println(s3==s4);
    }

operation result:
insert image description here

The diagram is as follows:
insert image description here

Note: The String class is a final class and cannot be inherited. Its member methods are final methods by default.
(In Java, a class modified by final is not allowed to be inherited, and the member methods in this class default to final methods)


Second, the comparison of String objects

There are 4 ways to compare strings in Java

1.==comparison

Note: For basic types, == compares the value in the variable, and for reference types == compares the address in the reference.
example 1:

public static void main(String[] args) {
    
    
	 int a = 10;
	 int b = 20;
	 int c = 10;
	 // 对于基本类型变量,==比较两个变量中存储的值是否相同
	 System.out.println(a == b); // false
	 System.out.println(a == c); // true
	 // 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
  	 String s1 = new String("hello");
     String s2 = new String("world");
     String s3 = "hello";
     String s4 = "hello";
     System.out.println(s1==s2);//false
     System.out.println(s3==s4);//true
}

2. equals() method (comparison in lexicographical order)

equals comparison: character by character in the String object (compared in the order of character size) , returns a value of type boolean.
Example 2:

public static void main(String[] args) {
    
    
	 String s1 = new String("hello");
	 String s2 = new String("hello");
	 String s3 = new String("Hello");
	 System.out.println(s1.equals(s2)); // true
	 System.out.println(s1.equals(s3)); // false
}

Screenshot of the result:
insert image description here

3. compareTo() method

Method prototype: int compareTo(String s)
Specific comparison method:
(1) First, compare according to the size of the dictionary order, if there are unequal characters, directly return the size difference between the two characters
(2) If the first k characters are equal ( k is the minimum length of two characters), the return value is the difference between the lengths of two strings
Example 3:

public static void main(String[] args) {
    
    
	 String s1 = new String("abc");
	 String s2 = new String("abc");
	 String s3 = new String("ac");
	 String s4 = new String("abcdef");
	 System.out.println(s1.compareTo(s2)); 
	 System.out.println(s1.compareTo(s3));
	 System.out.println(s1.compareTo(s4)); 
}

Screenshot of the result:
insert image description here

4. compareToIgnoreCase() method

Method prototype: int compareToIgnoreCase(String str)
is the same as compareTo, but ignores case comparison
Example 4:

public static void main(String[] args) {
    
    
	 String s1 = new String("abc");
	 String s2 = new String("ABc");
	 String s3 = new String("ac");
	 String s4 = new String("abcdef");
	 System.out.println(s1.compareToIgnoreCase(s2)); 
	 System.out.println(s1.compareToIgnoreCase(s3)); 
	 System.out.println(s1.compareToIgnoreCase(s4)); /
}

Screenshot of the result:
insert image description here


3. String search

The following are common search methods provided by the String class:
(1) char charAt(int index) : Returns the character at the index position. If the index is negative or out of bounds, an IndexOutOfBoundsException is thrown.
(2) int indexOf(int ch): Returns the position where ch first appears, and does not return -1.
(3) int indexOf(int ch, int fromIndex): Find the position where ch first appears from the position of fromIndex, without returning -1.
(4) int indexOf(String str): Returns the position where str appears for the first time, and does not return -1.
(5) int indexOf(String str, int fromIndex): Find the first occurrence of str from the position of fromIndex, without returning -1.
(6) int lastIndexOf(int ch): Search from the back to the front, return the position where ch first appeared, and return -1 if not.
(7) int lastIndexOf(int ch, int fromIndex): Search from the position of fromIndex, and search for the position where ch first appeared from the back to the front, and -1 is not returned.
(8) int lastIndexOf(String str): Search from the back to the front, return the first occurrence of str, and return -1 if not.
(9) int lastIndexOf(String str, int fromIndex):Start searching from the fromIndex position, and find the position where str first appears from the back to the front, and it does not return -1.
example:

 public static void main(String[] args) {
    
    
        String s = "abcaaddccbbcccaaddcc";
        System.out.println(s.charAt(5)); // 'd'---从0开始
        System.out.println(s.indexOf('c')); // 2
        System.out.println(s.indexOf('c', 10)); // 11
        System.out.println(s.indexOf("ccc")); // 11
        System.out.println(s.indexOf("dd", 10)); // 16
        System.out.println(s.lastIndexOf('c')); // 19
        System.out.println(s.lastIndexOf('c', 10)); // 8
        System.out.println(s.lastIndexOf("bb")); // 9
        System.out.println(s.lastIndexOf("cc", 10)); // 7
    }

Four, string conversion

1. Conversion of int to string

(1) Convert int to string
Example 1:

int a = 123;
String s = "" + a;   //直接拼接 “”

Example 2:

int a =123;
String s = String.valueOf(a); //调用String的valueOf()方法

Example 3:

int a = 123;
String s = Integer.toString(a);//调用整型包装类Integer的toString()方法

(2) String to int

Example 1:

String s = "1234";
int a = Integer.parseInt(s);//调用整型包装类Integer的parseInt()方法

Example 2:

String s = "1234";
int a = Integer.valueOf(s).intValue();

2. Conversion of numeric values ​​and strings

example:

public static void main(String[] args) {
    
    
	 // 数字转字符串
	 String s1 = String.valueOf(1234);
	 String s2 = String.valueOf(1.48);
	 String s3 = String.valueOf(true);
	 System.out.println(s1);
	 System.out.println(s2);
	 System.out.println(s3);
	 // 字符串转数字
	 int data1 = Integer.parseInt("1234");
	 double data2 = Double.parseDouble("12.34");
	 System.out.println(data1);
	 System.out.println(data2);
}

Screenshot of the result:
insert image description here

3. Case conversion

example:

public static void main(String[] args) {
    
    
        String s1 = "hello";
        String s2 = "HELLO";
        System.out.println(s1.toUpperCase());//小写转大写
        System.out.println(s2.toLowerCase());// 大写转小写
    }

4. String to array

example:

public static void main(String[] args) {
    
    
	 String s = "hello";
	 // 字符串转数组
	 char[] ch = s.toCharArray();
	 for (int i = 0; i < ch.length; i++) {
    
    
	 System.out.print(ch[i]);
	 }
	 System.out.println();
	 // 数组转字符串
	 String s2 = new String(ch);
	 System.out.println(s2);
}

Summarize

That's all for this article.

Guess you like

Origin blog.csdn.net/m0_53689542/article/details/124534832