Friends' favorite String!!!

Keywords: Java C html

catalogue

Characteristics of string:

Create string by:

String comparison

Common methods to get from String:

String interception method:

The common methods related to conversion in String are:

String and int

int to String

String to int:

Characteristics of string:

  1. The contents of the string are never mutable. [key points]
  2. Because the string cannot be changed, the string can be shared.
  3. String effect is equivalent to char [] character array, but the underlying principle is byte [] byte array.

Create string by:


Three construction methods:
public String(): creates a blank string without any content.
public String(char[] array): create the corresponding string according to the contents of the character array.
public String(byte[] array): creates a corresponding string according to the contents of the byte array.
A direct creation method:
String str = “Hello”; // Use double quotation marks directly on the right
 

String comparison

String constant pool [in heap memory]: the double quoted string written directly in the program is in the string constant pool. For basic types, = = is to compare values.
For reference types, = = is used to compare address values.

public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "abc";

        char[] charArray = {'a', 'b', 'c'};
        String str3 = new String(charArray);

        System.out.println(str1 == str2); // true
        System.out.println(str1 == str3); // false
        System.out.println(str2 == str3); // false
    }

equals is true only if the parameter is a string and the content is the same; Otherwise, false is returned.
When comparing constants and variables, it is recommended that constants be written in front

"abc".equals(str);
public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        char[] charArray = {'H', 'e', 'l', 'l', 'o'};
        String str3 = new String(charArray);

        System.out.println(str1.equals(str2)); // true
        System.out.println(str2.equals(str3)); // true
        System.out.println(str3.equals("Hello")); // true
        System.out.println("Hello".equals(str1)); // true

        String str4 = "hello";
        System.out.println(str1.equals(str4)); // false
        System.out.println("=================");

        String str5 = null;
        System.out.println("abc".equals(str5)); // Recommendation: false
//        System.out.println(str5.equals("abc")); //  Not recommended: error, null pointer exception, NullPointerException
        System.out.println("=================");

        String strA = "Java";
        String strB = "java";
        System.out.println(strA.equals(strB)); // false, case sensitive
        System.out.println(strA.equalsIgnoreCase(strB)); // true, ignoring case

        // Note that only English letters are case sensitive, and others are not case sensitive
        System.out.println("abc I 123".equalsIgnoreCase("abc One 123")); // false
    }

Common methods to get from String:

1. public int length(): Get the number of characters contained in the string and get the length of the string.
   str.length();

2. public String concat(String str): Splices the current string and the parameter string into a new string with a return value.
   str1 = "A"; str2 = "B";
   str3 = str1.concat(str2);//str3 = "AB"

3. public char charAt(int index): Gets a single character at the specified index position. (the index starts at 0.)
   str.charAt(0);//Gets the first character in str

4. public int indexOf(String str): Find the index position of the parameter string that appears for the first time in this string. If it does not return-1 Value.
   str.indexOf("C");//The index position of the first occurrence of C in str is - 1

String interception method:


public String substring(int index): intercept from the parameter position to the end of the string and return a new string.
public String substring(int begin, int end): intercept the string from begin to end.
Note: [begin, end], including the left, excluding the right.
 

public static void main(String[] args) {
        String str1 = "HelloWorld";
        String str2 = str1.substring(5);
        System.out.println(str1); // HelloWorld is intact
        System.out.println(str2); // World, new string
        System.out.println("================");

        String str3 = str1.substring(4, 7);
        System.out.println(str3); // oWo
        System.out.println("================");

        // In the following way, the content of the string remains unchanged
        // There are two strings: "Hello", "Java"
        // The address value is stored in strA.
        // The original address value was 0x666 of Hello,
        // Later, the address value became 0x999 in Java
        String strA = "Hello";
        System.out.println(strA); // Hello
        strA = "Java";
        System.out.println(strA); // Java
    }

The common methods related to conversion in String are:


public char[] toCharArray(): splits the current string into a character array as the return value.
char[] chars = "Hello".toCharArray();
public byte[] getBytes(): get the byte array at the bottom of the current string.
byte[] bytes = "abc".getBytes();
public String replace(CharSequence oldString, CharSequence newString):
Replace all existing old strings with new strings, and return the new string after replacement.
Note: CharSequence means that string types are acceptable
Replace the example curse shielding keyword with*
 

String and int

int to String

int i = 100;
	String s = "";
	s = String.valueOf(i);

String to int:

 String s = "123";
        int i1;
        i1 = Integer.parseInt(s);//The first method is recommended, which directly calls static methods
        int i2;
        i2 = Integer.valueOf(s).intValue();//The second method calls the first method internally, but more objects will be generated

        System.out.println(i1);
        System.out.println(i2);

Posted by waiwai933 on Sat, 09 Oct 2021 02:20:54 -0700