zoukankan      html  css  js  c++  java
  • Java基础知识强化32:String类之String类的判断功能

    1. String类的判断功能:

    boolean  equals (Object  obj )
    boolean  equalsIgnoreCase (String str )
    boolean  contains (String str )
    boolean  startsWith (String str )
    boolean  endsWith (String str )
    boolean  isEmpty()

    2. 案例:

     1 package cn.itcast_03;
     2 
     3 /*
     4  * String类的判断功能:
     5  * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
     6  * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
     7  * boolean contains(String str):判断大字符串中是否包含小字符串
     8  * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
     9  * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
    10  * boolean isEmpty():判断字符串是否为空。
    11  * 
    12  * 注意:
    13  *         字符串内容为空和字符串对象为空。
    14  *         String s = "";  //字符串内容为空
    15  *         String s = null;//字符串对象为空
    16  */
    17 public class StringDemo {
    18     public static void main(String[] args) {
    19         // 创建字符串对象
    20         String s1 = "helloworld";
    21         String s2 = "helloworld";
    22         String s3 = "HelloWorld";
    23 
    24         // boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
    25         System.out.println("equals:" + s1.equals(s2));
    26         System.out.println("equals:" + s1.equals(s3));
    27         System.out.println("-----------------------");
    28 
    29         // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
    30         System.out.println("equals:" + s1.equalsIgnoreCase(s2));
    31         System.out.println("equals:" + s1.equalsIgnoreCase(s3));
    32         System.out.println("-----------------------");
    33 
    34         // boolean contains(String str):判断大字符串中是否包含小字符串
    35         System.out.println("contains:" + s1.contains("hello"));
    36         System.out.println("contains:" + s1.contains("hw"));
    37         System.out.println("-----------------------");
    38 
    39         // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
    40         System.out.println("startsWith:" + s1.startsWith("h"));
    41         System.out.println("startsWith:" + s1.startsWith("hello"));
    42         System.out.println("startsWith:" + s1.startsWith("world"));
    43         System.out.println("-----------------------");
    44 
    45         // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩
    46 
    47         // boolean isEmpty():判断字符串是否为空。
    48         System.out.println("isEmpty:" + s1.isEmpty());
    49 
    50         String s4 = "";
    51         String s5 = null;
    52         System.out.println("isEmpty:" + s4.isEmpty());
    53         // NullPointerException
    54         // s5对象都不存在,所以不能调用方法,空指针异常
    55         System.out.println("isEmpty:" + s5.isEmpty());
    56     }
    57 }

    运行结果如下:

  • 相关阅读:
    爬虫大作业
    熟悉常用的HDFS操作
    数据结构化和保存
    爬取全部校园新闻
    爬取校园新闻
    Google布隆过滤器
    谷歌json和对象转换
    postgresql和postgis
    json和实体类互相转换
    Linux安装docker-compose
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4821547.html
Copyright © 2011-2022 走看看