Java learning -- day11_ Introduction to strings and regular expressions

Java learning - day11_ Introduction to strings and regular expressions

String [meeting]

A string is an ordered sequence of several characters. use String To represent a string.
The contents of the string, enclosed in double quotation marks. In double quotation marks, there is no limit to the number of characters, which can be 0 or 1
 One or more.

String class:java The string related functions are object-oriented,The corresponding class is formed--String class.

For example, the most common:
String str1 = "hello world";

Classification of strings

Immutable string:
  • Corresponding class: String
  • Features: the string itself cannot be changed, which has nothing to do with the reference to the string
  • Using '' directly creates an immutable string
Variable string:
  • Corresponding class: StringBuilder/StringBuffer
  • Features: the string itself can change, independent of the reference to the variable string
    Create variable string
  • StringBuffer stringBuffer = new StringBuffer("hello world");
Memory analysis of string:

String is a reference data type. But the string reference is a little different from the previous reference in the object-oriented part
No.

Difference: the object of the class is the space opened directly on the heap. String is the space opened up in the constant pool. (constant)
Pool, which is a subspace in the method area)

String str = new String("hello world");
  • String is a class used to describe strings in Java, which has construction methods.
  • Through the construction method provided by the String class, the instantiated String object opens up space on the heap. In heap space, there is an internally maintained attribute that points to a space in the constant pool.
// A space for String objects is opened up on the heap, and the address of the space on the heap is given to str1
// In heap space, there is an internal attribute that points to "hello world" in the constant pool
String str1 = new String("hello world");
 
// A space for String objects is opened up on the heap, and the address of the space on the heap is given to str2
// In heap space, there is an internal attribute that points to "hello world" in the constant pool
String str2 = new String("hello world");
 
System.out.println(str1 == str2);       // false: because the addresses of the two heap spaces are stored in str1 and str2.
System.out.println(str1.equals(str2));     // true: because the equals method has been overridden in the String class, the overridden implementation is to compare the String in the constant pool actually pointed to.

ps: try to use equals for String comparison in the future

Memory analysis of string splicing

  • Directly use two string literals for splicing

    • In fact, it is to splice two strings directly enclosed in double quotation marks. Similar to String str
      = "hello" + "world".
    • Here, space operations are performed directly in the constant pool. The address of the spliced result in the constant pool is given to str for processing
      Assignment.
  • Use a string variable and other to splice

    String s = new String("hello");

    String s1 = s + "world";

    • The splicing here is not completed directly in the constant pool.
    • In this splicing process, an object of String class is implicitly instantiated, opening up space on the heap.
      The space on the heap internally maintains an attribute that points to the splicing result in the constant pool. The space on this pile
      The address is assigned to the reference on the left.

Common methods of string

Construction method of string

  • String constructor enumeration
Construction methodMethod description
String()Parameterless construction, instantiating an empty string object. The so-called null character
String is actually '', not null.
String(String str)Instantiate another string through one string.
String(char[] arr)Instantiate a string through a character array. Put the in the character array
All the characters are spliced together.
String(char[] arr, int
offset, int count)
Instantiate a string through a character array. Put the in the character array
The characters in the specified range are spliced together.
String(byte[] arr)Instantiate a string through a byte array. Put the in the byte array
All bytes are spliced into a string.
String(byte[] arr, int
offset, int count)
Instantiate a string through a byte array. Put the in the byte array
Concatenate the bytes of the specified range into a string.
public class StringMethod1 {
    public static void main(String[] args) {
        // 1. Parameterless construction, instantiating an empty string object. The so-called empty string is actually "", not null.
        String s1 = new String();   // String s1 = "";
        System.out.println(s1);
 
        // 2. Instantiate another string through one string.
        String s2 = new String("hello");
        System.out.println(s2);
 
        char[] arr1 = { 'h', 'e', 'l', 'l', 'o' };
        // 3. Instantiate a string through a character array. Splice all the characters in the character array together.
        String s3 = new String(arr1);
        System.out.println(s3);
        // 4. Instantiate a string through a character array. Concatenates a specified range of characters in a character array.
        String s4 = new String(arr1, 2, 3);
        System.out.println(s4);
 
        byte[] arr2 = { 97, 98, 99, 100, 101, 102, 103, 104 };
        // 5. Convert each byte in a byte array into corresponding characters, and then splice them together to form a string.
        String s5 = new String(arr2);
        System.out.println(s5);
        // 6. Start with the offset bit in a byte array, take length bytes, convert each byte into corresponding characters, and then splice them together to form a string.
        String s6 = new String(arr2, 2, 4);
        System.out.println(s6);
    }
}

Non static method of string

Because strings are constants. Any operation to modify the string will not affect the modified string
Ring. All string modification operations actually instantiate a new string object. In this new string
In, the modified results are stored. And return the new string as a return value. So, if necessary
To get the result of modifying a string, you need to receive the return value of the method.

Return valuemethodMethod description
Stringconcat(String str)String splicing Splices one string with another and returns the spliced result
Stringsubstring(int beginIndex)String interception. Start with beginIndex and intercept to the end of the string.
Stringsubstring(int beginIndex, int endIndex)String interception. Intercept the substring within the range of [beginIndex, endIndex) in the string.
Stringreplace(char oldChar, char newChar)String substitution. Replace all old characters in the original string with new characters.
Stringreplace(CharSequence old, harSequence newC)String substitution. Replace all the old character sequences in the original string with the new character sequence.
charcharAt(int index)Character acquisition. Gets the character that refers to the location.
char[]toCharArray()Converts a string to an array of characters.
byte[]getBytes()Converts a string into a byte array.
intindexOf(char c)Gets the subscript of the first occurrence of a character in a string. If not, return - 1
intindexOf(char c, int fromIndex)Gets the first subscript of a character in a string starting from the fromIndex bit.
intlastIndexOf(char c)Gets the subscript of the last occurrence of a character in a string.
intlastIndexOf(char c, int fromIndex)Gets the subscript of the last occurrence of a character in a string starting from the fromIndex bit.
StringtoUppeerCase()Converts all lowercase letters in a string to uppercase letters.
StringtoLowerCase()Converts all uppercase letters in a string to lowercase letters
booleanisEmpty()Determines whether a string is an empty string.
intlength()Gets the length of a string.
booleancontains(String str)Judge whether a string contains another string.
booleanstartsWith(String prefix)Determines whether a string starts with the specified string.
booleanendsWith(String shuffix)Determines whether a string ends with the specified string.
Stringtrim()Remove spaces at the beginning and end of a string.
booleanequals(Object obj)Judge whether the contents of two strings are the same.
booleanequalsIgnoreCase(String str)Judge whether the contents of two strings are the same, ignoring case.
intcompareTo(String other)Compares the size of two strings.
intcompareToIgnoreCase(String other)Compares the size of two strings, ignoring case.
public class StringMethod2 {
    public static void main(String[] args) {
        // 1. String splicing is more efficient than plus splicing
        String ret1 = "hello".concat("world");
        System.out.println(ret1);       // helloworld
 
        // 2. String interception
        String ret2 = "hello world".substring(3);
        System.out.println(ret2);       // lo world
        String ret3 = "hello world".substring(3, 8);
        System.out.println(ret3);       // lo wo
        // *. character sequence interception is not much different from string interception
        CharSequence charSequence = "hello world".subSequence(3, 8);
        System.out.println(charSequence);
 
        // 3. Replace all old characters in the original string with new characters
        String ret4 = "hello world".replace('l', 'L');
        System.out.println(ret4);       // heLLo worLd
        // 4. Replace all the old character sequences in the original string with the new character sequence
        String ret5 = "hello world".replace("ll", "~");
        System.out.println(ret5);       // he~o world
 
        // 5. Convert to character array
        char[] ret6 = "hello world".toCharArray();
        System.out.println(Arrays.toString(ret6));      // [h, e, l, l, o,  , w, o, r, l, d]
        // 6. Convert to byte array
        byte[] ret7 = "hello world".getBytes();
        byte[] ret7 = "hello world".getBytes("utf8");
        System.out.println(Arrays.toString(ret7));      // [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
 
 
        // 7. Get the subscript of a character for the first time in a string. (left to right)
        //c:/a/b/c/bing.png
        int ret8 = "hello world".indexOf('L');
        System.out.println(ret8);                   // 2
        // 8. Get the subscript of a character that appears for the first time after the fromIndex bit in a string.
        int ret9 = "hello world".indexOf('l', 4);
        System.out.println(ret9);                   // 9
        // 9. Get the subscript of the last occurrence of a character in a string. (right to left)
        int ret10 = "hello world".lastIndexOf('o');
        System.out.println(ret10);                  // 7
        // 10. Get the subscript of the last occurrence of a character in a string starting from the fromIndex bit
        int ret11 = "hello world".lastIndexOf('o', 5);
        System.out.println(ret11);                  // 4
 
        // 11. String case conversion
        System.out.println("hello WORLD".toUpperCase());
         System.out.println("hello WORLD".toLowerCase());
 
        // 12. Judge whether a string contains another string.
        System.out.println("hello world".contains("loo"));
 
        // Requirement: judge whether a string contains a character
        // Answer: or the subscript of this character in the string. If it is not - 1, it indicates that it contains.
 
        // 13. Judge whether a string starts with the specified string.
        //give an example: http://www.baidu.com:8080/a/g/d?name=zhangsan
        //Website structure: protocol: / / domain name (ip address): port number / resource path? Query criteria (structure: key1 = value1 & key2 = Value2)
        /*
        Protocol: the same specification. The unified protocol used for application layer user communication is http/https    
https:Secure http protocol
        Domain name: a name given to the host. The domain name will be mapped to the host IP When we visit the domain name, the system
 The system will automatically go to the DNS server to find out whether there is an ip mapping corresponding to the current domain name Find and use the mapped ip access directly
 Server, not found, display unreachable
        A host has a unique ip address, and the domain name corresponds to ip one by one We can find it according to the domain name
 To the specified host
        DNS Server: it contains the mapping relationship between all registered ip and domain names
        Port number: determines a server (app) on a host
        Value range of port number (065535)
        Resource path: the path of the resource on the server
        Query condition: the requirement description of the client
        
        Note: components that can be omitted:
        Protocol default http
        port 
        Resource path
        query criteria
        */
        System.out.println("Harry Potter and the Sorcerer's Stone.mp4".startsWith("Harry Potter"));
        System.out.println("Harry Potter and the Sorcerer's Stone.mp4".endsWith(".mp4"));
 
        // 14. Remove the spaces at the beginning and end of a string
        System.out.println("        hello world        ".trim());
 
        // 15. Judge whether the contents of two strings are the same
        System.out.println("hello world".equals("HELLO WORLD"));
        System.out.println("hello world".equalsIgnoreCase("HELLO WORLD"));  // true
        // 16. Compare the size of two strings
        // General logic:
        // >0: Previous string > parameter string
        // ==0: two strings are equal in size
        // < 0: Previous string < parameter string
 
        /*
         * Dictionary order: compare the current two characters according to the ASCII table. If the ASCII code is large, it is considered to be a large character
         * Rule: compare from the first character on the left
         * If the current characters are different, it is directly considered that the ASCII large string is a large string, and the subsequent characters stop
 Stop comparison
         * Specific rules for current character comparison: use the preceding character - the following character, and return the difference If it's negative
 Number, indicating that the preceding string is smaller than the following string On the contrary, the front is big
         * If the current character is the same, compare the second character and push it back in turn. If the comparison is the same at the end,
It is considered that the two strings are equal, and the difference returns 0
         * If one of the two strings is a substring from the other The result of the comparison is the difference in the number of characters
 Value. If the substring is followed, the difference is a positive number,
         * The preceding is a substring, and the difference is negative
         */
 
        int result = "hello world".compareTo("hh");
        System.out.println(result);
      
        //17. Cutting: String[] split(String)
        String s6 = "h.e.l.l.o";
        //When we use a character in a string as a knife, it is no longer content
        String[]  strings1 = s6.split("\\.");//. is a special character that represents any
        for (String ss:strings1) {
            System.out.println(ss);
        }
        
        reflection:There is a string "hello" Cut with empty string,What is the result?
    }
}
Static method of string
  • Common static methods

  • Return valuemethoddescribe
    Stringjoin(CharSequencedelimiter, CharSequenceelements)Several strings are spliced together. When splicing, elements are separated by the specified separator.
    Stringformat(String format, Object... args)Format the string in the specified format
    public class StringMethod3 {
        public static void main(String[] args) {
     
            // Several strings are spliced together. When splicing, elements are separated by the specified separator
            String str1 = String.join(", ", "lily", "lucy", "uncle wang", 
    "polly");
            System.out.println(str1);
     
            float score = 100;
            String name ="xiaoming";
            int age = 19;
            // Hello, my name is xiaoming. I'm 19 years old. I got 100 points in this exam.
            /**
             * Common placeholders:
             * %s : Substitute string - >% ns: enough n-bit string. If not, fill in spaces
             * %d : Integer number placeholder - >% Nd: round up enough n bits, and fill in spaces if not enough.
             * %f : Floating point number placeholder - >% NF: keep the number after the decimal point
             * %c : Character placeholder
             */
            String str2 = String.format("Hello, my name is%11s,this year%03d Years old, I passed the exam%.6f Points.", name, age, score);
             System.out.println(str2);
        }
    }
    

    StringBuffer and StringBuilder classes

    Are classes used to manipulate strings, which we call variable strings
    Strings are constants. All methods of operating strings cannot directly modify the string itself. If we need to
    To the modified result, you need to receive the return value.
    StringBuffer and StringBuilder are not string classes, but classes used to manipulate strings. A word is maintained in the class
    String properties. These strings operate the methods in the class. You can directly modify the value of this property. For the user,
    You can get the result of the operation without returning the value.
    In the StringBuffer or StringBuilder class, a character array is maintained. All the operation methods in these classes,
    Are operations on this character array.

    Return valuecommon methodMethod description
    Construction method ()Instantiate a string operation class object, which operates on an empty string.
    Construction method (String str)Instantiate a string operation class object, which operates on a specified string.
    StringBuffer/StringBuilderappend(...)Splice a piece of data to the end of an existing string
    StringBuffer/StringBuilderinsert(int offset, ...)Inserts a data into the finger position of a string.
    StringBuffer/StringBuilderdelete(int start, int end)Delete the data within the range of [start, end] in a string. If start is out of bounds, a subscript out of bounds exception will occur. If end is out of bounds, it will not affect, and all contents behind the string will be deleted.
    StringBuffer/StringBuilderdeleteCharAt(int index)Deletes the character with the specified subscript.
    StringBuffer/StringBuilderreplace(int start, int end, String str)Replace: replace the data within the range of [start, end] in the string with the specified string.
    voidsetChatAt(int index, char c)Replace the character of the specified subscript with a new character.
    StringBuffer/StringBuilderreverse()Invert and flip a string back and forth.
    StringtoString()Returns a string that is being manipulated.
    public class Test {
        public static void main(String[] args) {
            // 1. Construction method
            StringBuilder sb = new StringBuilder("hello world");
     
            // 2. Add: splice other strings after one string
            sb.append('!');
     
            // 3. Add: insert a piece of data in the specified subscript
            sb.insert(3, "AAAAA");
     
            // 4. Delete: delete data within the range of [start, end] in the string
            sb.delete(3, 5);
     
            // 5. Delete: delete the character that refers to positioning
            sb.deleteCharAt(6);
     
            // 6. Intercept a part of the string. This operation will not be modified to itself. If you want to get the intercepted part, you need to receive the return value.
            String sub = sb.substring(4, 6);
     
            // 7. Replace: replace the data within the range of [start, end] in the string with the specified string
            sb.replace(3, 6, "l");
     
            // 8. Modify the character of the specified subscript
            sb.setCharAt(0, 'H');
     
            // 9. Flip the string back and forth
            sb.reverse();
     
            System.out.println(sb);
        }
    }
    

    difference

    StringBuffer and StringBuilder are as like as two peas. But there are differences between them:

    • StringBuffer is thread safe
    • StringBuilder is thread unsafe.
    Usage scenario
    • In a multithreaded environment, when multiple threads operate on this object at the same time, StringBuffer is used.
    • When you are not in a multithreaded environment and only one thread operates on this object, use StringBuilder.

ps: any usage scenario involving string operation, especially the string operation in the loop. Do not use
String method, using StringBuffer or StringBuilder method to do.

    • Since the String itself is immutable, all modification operations of the String class are actually instantiated in the method
      A new string object that stores the address of the spliced new string and returns the new string. If operation ratio
      More frequently, it means that a large number of temporary strings are instantiated and destroyed, which is extremely inefficient. StringBuffer,
      Unlike StringBuilder, it internally maintains a character array around which all operations are carried out
      Operation. The toString() method is called for conversion only when it needs to be converted into a string. When string manipulation is frequently used
      When doing, there is no intermediate temporary string, which is more efficient.

Regular expression [understand]

Regular expressions, not Java Unique. It is a set of independent and systematic knowledge points. In many languages, there are
 There is a use of regular.
 
Regular expressions are used for string verification and matching. In fact, regular expressions have only one function: Verify that a string is
 No matches the specified rule.
 
However, in many languages, other functions are added on the basis of matching. for example Java: In matching base
 In addition, deletion and replacement are added... Function.

Use of regular expressions

The same functions can be realized by String, StringBuffer and StringBuilder, or by regular expression
realization.

Matching rules for regular expressions

Match character by character to determine whether it is consistent with the rules defined in the regular expression.

Metacharacter

Metacharactersignificance
^Matches the beginning of a string. In Java regular matching, you can omit not writing.
$Matches the end of a string. In Java regular matching, you can omit not writing
[]Match one character.
[abc]: the character representing this bit, which can be a, b or c.
[a-z]: the character representing this bit, which can be any character within the range of [a, z].
[a-zABC]: the character representing this bit, which can be any character within the range of [a,z], or A, B, or C.
[a-zA-Z]: the character representing this digit can be any letter, including uppercase and lowercase letters.
[^ a-z[hk]]: the character representing this bit cannot be any lowercase letter, except h and k.
[^ abc]: the character representing this bit, which can be any character except a, b and c.
\Escape character. Because regular expressions need to be written in a string in Java. And in the string
\It is also an escape character. Therefore, when writing regular expressions in Java, the escape characters are \
Make some special characters become ordinary characters, and rules can be specified.
Make some ordinary characters have special meanings.
\dMatch all numbers, equivalent to [0-9].
\DMatch all non numbers, equivalent to [^ 0-9].
\wMatch all word characters, equivalent to [a-zA-Z0-9_].
\WMatch all non word characters, equivalent to [^ a-zA-Z0-9].
.Wildcard, which can match any character.
+The preceding bit or group of characters appears one or more times in succession.
?The preceding bit or group of characters appear one or zero times in a row.
*The preceding bit or group of characters appears zero, one or more times in a row.
{}An accurate match of the number of occurrences of the preceding bit or group of characters.
{m} : indicates that the preceding bit or group of characters appear m times in succession.
{m,}: indicates that the preceding bit or group of characters appear at least m times in a row.
{m,n}: indicates that the previous bit or group of characters appear continuously at least m times and at most N times.
It acts on the whole or a group to represent the matching content, which can be any part.
|abc|123|opq: represents the whole part. It can be abc, 123 or OPQ
()grouping. Treat some consecutive characters as a whole.

Pattern and Matcher classes

Pattern class: in Java, the carrier of regular expressions. Use regular expressions to verify, cut and replace strings
In other words, you need to use this class.

Modifier & return valuemethoddescribe
static booleanmatches(String regex, CharSequence sequence)Static rule verification, which directly verifies whether a string conforms to a rule
static Patterncompile(String regex)Compile a string into a Pattern object, which can be used as a regular expression.
String[]split(CharSequence sequence)A character sequence is cut according to the specified rules to get each cut part.
String[]split(CharSequence sequence, int limit)A character sequence is cut according to the specified rules into a specified number of segments to obtain each cutting part.
Matchermatcher(CharSequence sequence)Verify a regular expression with a string.

Matcher class: in Java, the result description of a regular check.

Keywords: Java string regex

Added by jmcneese on Wed, 05 Jan 2022 03:19:52 +0200