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 }

}

String的用法——判断功能的更多相关文章

  1. Java基础知识强化32:String类之String类的判断功能

    1. String类的判断功能: boolean equals (Object obj ) boolean equalsIgnoreCase (String str ) boolean contain ...

  2. 字符串类String类的判断功能

    StringDemo.java /* * Object:是类层级结构中的根类,所有的类都直接或间接的继承自该类. * 如果一个方法的形式参数是Object,那么这里我们就可以传递它的任意的子类对象. ...

  3. String类的判断功能

    /* * Object:是类层级结构中的根类,所有的类都直接或间接的继承自该类. * 如果一个方法的形式参数是Object,那么这里我们就可以传递它的任意的子类对象. * * String类的判断功能 ...

  4. String的用法——转换功能

    package cn.itcast_05; /* String类的转换功能: byte[] getByte():把字符串转换成字节数组 复习: public String(byte[] bytes): ...

  5. String的用法——其他功能

    package cn.itcast_06; /* String类的其他功能: 替换功能: String replace(char old,char new) String replace(String ...

  6. String的用法——获取功能

    package cn.itcast_04; /* String类获取功能 int length():获取字符的长度 char charAt(int index):获取指定索引位置的字符 int ind ...

  7. String 类的其他功能

    12.01_常见对象(Scanner的概述和方法介绍)(掌握) A:Scanner的概述 B:Scanner的构造方法 Scanner(InputStream source) System.in C: ...

  8. test命令用法。功能:检查文件和比较值

    test命令用法.功能:检查文件和比较值 1)判断表达式 if test  (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2                  两个表达 ...

  9. Java基础知识强化72:正则表达式之判断功能(手机号码判断 和 校验邮箱)

    1.  判断功能: 使用了String类的matches方法,如下: public boolean matches(String regex): 2. 判断手机号码的案例: package cn.it ...

随机推荐

  1. linux document and directory find

    http://suchalin.blog.163.com/blog/static/55304677201062924959497/ Linux 查看文件夹大小及文件数量命令 2010-07-29 14 ...

  2. ABAP 邮件

    function zint_send_email.*"-------------------------------------------------------------------- ...

  3. [10.26_P2] 最短路 (单源最短路应用)

    单源最短路问题拓展 Description 给你一张图,图上有 n 个点,m 条边,要你找到两个点,使其最短路恰好包含给定的 k 个点.输出这条最短路的长度,输入保证有解. 输入格式 第一行两个数 n ...

  4. lucene 索引文件大小分布_tim

    Hi, I have index ~31G where27% of the index size is .fdt files (8.5G)20% - .fdx files (6.2G)37% - .f ...

  5. 并不对劲的cdq分治解三维偏序

    为了反驳隔壁很对劲的太刀流,并不对劲的片手流决定与之针锋相对,先一步发表cdq分治解三维偏序. 很对劲的太刀流在这里->  参照一.二维偏序的方法,会发现一位偏序就是直接排序,可以看成通过排序使 ...

  6. [noip模拟赛]午餐

    https://www.zybuluo.com/ysner/note/1325779 题面 一共有\(n\)个人,林先森知道开始时只有\(1\)号会毒瘤算法.林先森了解到很多人一起吃过 午餐;具体地, ...

  7. JSP+MySQL实例

    转自:https://www.yiibai.com/jsp/jsp_mysql.html 在本章中,我们将讨论如何使用JSP访问数据库(这里以MySQL数据库为例).并假设您对JDBC应用程序的工作方 ...

  8. git合并相关问题(copy)

    [说明:资料来自http://gitbook.liuhui998.com/3_3.html] 一个Git仓库可以维护很多开发分支.现在我们来创建一个新的叫”experimental”的分支: $ gi ...

  9. Gym 100531A Alarm Clock (水题)

    题意:给定一个被高亮的数,问你是不是有个时间恰好高亮是这个数. 析:直接暴力,直接暴力,枚举每一位时间,当然也可以枚举时间,能找到就是有,找不到就算了. 代码如下: #pragma comment(l ...

  10. Ruby  Array类

    Array类 更新: 2017/06/03 更新: 2017/08/31 更新sort的详细内容 更新: 2017/09/07 增加each_with_index, each.with_index(o ...