String foundation in java

String class (1)

When using the String class, there is no need to guide the package. All strings in the program are instances of the String class, that is, all double quoted strings in the java program are String class objects.

String construction method

  1. public String() creates a blank string object
  2. public String(char[] arr) creates a string object
  3. String s = "abc" create string object by direct assignment

There are two ways to create a String object

  1. Create a string object through new. Each time new will apply for a memory space. Even if the contents of the two string objects are the same, the addresses are different.

    char[] arr={'h','e','l','l','o'};
    String s1=new String(arr);
    String s2=new String(arr);
    
  2. As long as the strings created in the '' mode are the same, no matter how many times the String appears, only one String object will be created.

    String s3="hello";
    String s4="hello";
    

    string comparison

    String is an object and cannot be compared directly with "= =". If it is a basic type, use "= =" to compare whether the data values are the same; If it is a reference type, compare whether the address values are the same.

    Strings are compared through the "equals()" method.

    package com.company.www;
    public class Main {
        public static void main(String[] args) {
            //Create with new
            String s1=new String("hello");
            String s2=new String("hello");
            //Create with ''
            String s3="happy";
            String s4="happy";
            if(s1==s2){
                System.out.println("s1 and s2 Same address");
            }else if(s1.equals(s2)){
                System.out.println("s1 and s2 The string content is the same");
            }
            if(s3==s4&&s3.equals(s4)){
                System.out.println("s3 and s4 The address and string contents are the same");
            }
        }
    }
    

    The output content is:

    The contents of s1 and s2 strings are the same
    The s3 and s4 addresses and string contents are the same

    This code once again proves that the same content string object created with new occupies different memory, and the same string object created with "" only occupies one address.

    Traversal of string

    The traversal of strings requires two String class methods: charAt(); And length();

    Example:

    package com.company.www;
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
          Scanner scanner=new Scanner(System.in);
          System.out.println("Please enter a string:");
          String s=scanner.nextLine();
          for (int i=0;i<s.length();i++){
              System.out.print(s.charAt(i));
          }
        }
    }
    

    The output result is:

    Please enter a string:
    Happy New Year
    Happy New Year

    Count the number of occurrences of different characters

    Let's go straight to the example

    package com.company.www;
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
          Scanner scanner=new Scanner(System.in);
          System.out.println("Please enter a string:");
          String s=scanner.nextLine();
          int big=0,small=0,number=0;
            //First, set three integer types to count the three character types
            for (int i=0;i<s.length();i++){
                char c=s.charAt(i);
                //Set a char type to receive a single character
                if(c>='a'&&c<='z'){
                    small++;
                }else if(c>='A'&&c<='Z'){
                    big++;
    
                }else if (c>='0'&&c<='9'){
                    number++;
                }
            }
            System.out.println("Lowercase letters have"+small+"individual");
            System.out.println("Capital letters have"+big+"individual");
            System.out.println("Number yes"+number+"individual");
        }
    }
    

    The output result is:

    Please enter a string:
    ilikestudy233
    There are 10 lowercase letters
    There are 0 uppercase letters
    There are three numbers

    String splicing

    Example: arbitrarily enter a string and splice it into the form of "* # * string * # *"

    package com.company.www;
    import java.util.Scanner;
    //Change the output string to * #* string * #* format
    public class Main {
        public static void main(String[] args) {
            System.out.println(pinjie());
        }
        public static String pinjie() {
            Scanner scanner=new Scanner(System.in);
            System.out.println("Please enter a string:");
            String s=scanner.nextLine();
            String a = "*#*";
            for (int i = 0; i <s.length(); i++) {
                if(i<s.length()){
                    a+=s.charAt(i);
                }
            }
            a+="*#*";
            return a;
        }
    }
    

    The output result is:

    Please enter a string:
    happy
    #happy#

Keywords: Java Back-end string

Added by shanksta13 on Wed, 05 Jan 2022 18:12:59 +0200