zoukankan      html  css  js  c++  java
  • String的用法——判断功能

    package cn.itcast_03;
    /*

    • String的判断功能:
    •  1.boolean equals(Object obj):字符串的内容是否相同,区分大小写
      
    •  2.boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
      
    •  3.boolean contains(String str):判断大字符串中是否包含小字符串
      
    •  4.boolean startsWith(String str):判断字符串是否以某个指定的字符串开始
      
    •  5.boolean endsWith(String str):判断字符串是否以摸个指定的字符串结尾
      
    •  6.boolean isEmpty():判断字符串是否为空
      
    • 注意:
    •  字符串为空和字符串对象为空不一样。
      
    •  String s = "";字符串为空
      
    •  String 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 contains(String str):判断大字符串中是否包含小字符串
    	System.out.println("contains:" + s1.contains("hell"));//true
    	System.out.println("contains:" + 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
    	
    }
    

    }

  • 相关阅读:
    WebPart 生存周期
    【Linq to SharePoint】对列表查询的分页技术
    新闻联播 代码
    首页顶部图片带Flash代码
    [翻译]简单谈谈事件与委托
    asp.net调试
    ASP.NET 2.0加密Web.config 配置文件
    网站用户登录和验证的资料
    Membership的一些资料
    asp.net网站登录的一些资料。
  • 原文地址:https://www.cnblogs.com/zhangyuestudying/p/10837107.html
Copyright © 2011-2022 走看看