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. 迅雷CTO李金波:致创业者的一封信

    我的创业感悟:写给正在寻找机会的你 李金波 我在迅雷的6年里,经历了许多困难.最折磨人的,是寻找人才:最惋惜的,莫过于看着优秀的人擦肩而过.今天再次创业(http://myhada.com),再次招聘 ...

  2. 【转载】在VS2008中使用WSE 3.0过程全记录

    WSE全称是Web Service Enhancement,提供了更好的安全性实现,以及大对象传输的设计. 有关WSE的一些介绍,如果不清楚,可以参考下面的链接 官方介绍:http://www.mic ...

  3. 1.5.4 HAVING子句

    1.5.4 HAVING子句正在更新内容.请稍后

  4. Codeforces 104C Cthulhu dfs暴力 || 点双连通缩点

    题目链接:点击打开链接 题意: 给定n个点m条边的无向图 问图中是否存在 有且仅有一个简单环和一些树,且这些树的root都在这个简单环上. 瞎写了个点双. . == #include <stdi ...

  5. 1.NetDh框架之数据库操作层--Dapper简单封装,可支持多库实例、多种数据库类型等(附源码和示例代码)

    1.NetDh框架开始的需求场景 需求场景: 1.之前公司有不同.net项目组,有的项目是用SqlServer做数据库,有的项目是用Oracle,后面也有可能会用到Mysql等,而且要考虑后续扩展成主 ...

  6. VS2010打开高版本VS解决方案

    http://blog.csdn.net/backspace110/article/details/62111273 Microsoft Visual Studio Solution File, Fo ...

  7. hadoop第一个例子WordCount

    hadoop查看自己空间 http://127.0.0.1:50070/dfshealth.jsp import java.io.IOException; import java.util.Strin ...

  8. Codeforces Round #261 (Div. 2)——Pashmak and Graph

    题目链接 题意: n个点.m个边的有向图.每条边有一个权值,求一条最长的路径,使得路径上边值严格递增.输出路径长度 )) 分析: 由于路径上会有反复点,而边不会反复.所以最開始想的是以边为状态进行DP ...

  9. XMU 1612 刘备闯三国之桃园结义 【二分】

    1612: 刘备闯三国之桃园结义 Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 181  Solved: 12[Submit][Status][We ...

  10. Linux MTD下获取Nand flash各个参数的过程的详细解析【转】

    本文转载自:https://www.crifan.com/files/doc/docbook/nand_get_type/release/html/nand_get_type.html 文章不错可以看 ...