-odeg string use function

package cn.itcast_03;
/*

  • String rule function:
  •  1
  •   2. Boolean equalsignorecase (String Str): это содержимое строки строки одинаково и игнорируйте нижний случай
  •   3. Boolean содержит (String Str): определите, содержит ли большая строка небольшая строка
  •   4. Boolean Startswith (String Str): определите, начинается ли строка с указанной строки
  •   5. Boolean Endswith (String Str): Определите, заканчивается ли строка указанной строкой
  •   6. Boolean isempty (): определить, пуста ли строка
  • notice:
  •   Строка пуста, а объект строки отличается.
  •   String s = ""; строка пуста
  •   Строка s = null; строковый объект пуст
  • */

public class StringDemo {

public static void main(String[] args) {
         // Создание объектов
    String s1 = "helloworld";
    String s2 = "helloworld";
    String s3 = "HelloWorld";
    String s4 = "hell";
    
         // Boolean Equals (Object obj): Содержание такого же строки?
    System.out.println("equals:" + s1.equals(s2));//true
    System.out.println("equals:" + s1.equals(s3));//false
    System.out.println("------------------------------------------------");
    
         // boolean equalsignorecase (string str): это содержимое строки строки одинаково и игнорируйте нижний случай
    System.out.println("equalsIgnoreCase:" + s1.equalsIgnoreCase(s2));//true
    System.out.println("equalsIgnoreCase:" + s1.equalsIgnoreCase(s3));//true
    System.out.println("------------------------------------------------");
    
         // Boolean содержит (String Str): определить, содержит ли большая строка небольшая строка
    System.out.println("contains:" + s1.contains("hell"));//true
         System.out.println ("содержит:" + s1.contains ("hw"); // false, символ должен быть соединен вместе
    System.out.println("contains:" + s1.contains("owo"));//true
    System.out.println("contains:" + s1.contains(s4));//true
    System.out.println("------------------------------------------------");
    
         // boolean startswith (string str): определить, начинается ли строка с указанной строки
    System.out.println("startsWith:" + s1.startsWith("h"));//true
    System.out.println("startsWith:" + s1.startsWith(s4));//true
    System.out.println("startsWith:" + s1.startsWith("world"));//false
    System.out.println("------------------------------------------------");
    
         // boolean endswith (string str): определить, заканчивается ли строка указанной строкой
    System.out.println("endsWith:" + s1.endsWith("h"));//false
    System.out.println("endsWith:" + s1.endsWith(s4));//false
    System.out.println("endsWith:" + s1.endsWith("world"));//true
    System.out.println("------------------------------------------------");
    
         // boolean isempty (): определить, пуста ли строка
    System.out.println("isEmpty:" + s1.isEmpty());//false
    
    String s5 = "";
    String s6 = null;
    System.out.println("isEmpty:" + s5.isEmpty());//true
         // ни один из объектов не существует, поэтому метод не может быть вызван
    System.out.println("isEmpty:" + s6.isEmpty());//NullPointerException
    
}

}

Reprinted from: https://www.cnblogs.com/zhangyuestudaying/p/10837107.html

Leave a Comment