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. 块状元素的text-align对齐属性

    能够为块状元素(div,h1,h2,form等)内容设置位置text-align:center,left;right;

  2. 解决Install failed uid changed

    出现此问题的原因大多是apk冲突造成,解决的办法如下: 将apk相关文件和相关内容删除 (1) 将手机root,Window上装root大师等工具root成功率较高 (2) 删除可能相关的文件:/da ...

  3. inter 也支持linux开发了

    http://www.intel.cn/content/www/cn/zh/intelligent-systems/bay-trail/atom-processor-e3800-family-over ...

  4. 当你使用LINQ做底层时,最好设计一个工厂,不要把LINQ的动作暴露给业务层

    1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ...

  5. UVA 437 The Tower of Babylon巴比伦塔

    题意:有n(n≤30)种立方体,每种有无穷多个.要求选一些立方体摞成一根尽量高的柱子(可以自行选择哪一条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽. 评测地址:http:/ ...

  6. unbantu16.04安装jdk

    1,解压缩jdk到指定目录 2,修改目录,方便使用 3,配置环境变量 sudo gedit /etc/environment 末尾加入以下配置(JAVA_HOME 后的路径就是jdk的文件位置) PA ...

  7. RDD基础

    RDD的两种操作 1.Tansformation(转化操作):返回值还是一个RDD 2.Action(行动操作):返回值不是一个RDD      第一种Transformation是返回一个新的RDD ...

  8. JSOI2010 联通数

    传送门 这道题的题目描述看起来很奇怪.实际上的意思是要求在这个有向图之内能到达的点对有多少,解释一下题里的图片就是(1,1),(1,2),(1,3),(1,4),(1,5),(2,2),(2,3),( ...

  9. 小程序-demo:小程序示例-page/component2

    ylbtech-小程序-demo:小程序示例-page/component2 以下将展示小程序官方组件能力,组件样式仅供参考,开发者可根据自身需求自定义组件样式,具体属性参数详见小程序开发文档. 1. ...

  10. UI:使用 pod 引入 AFNetworking

    cocdpods的安装  参考1  参考2 参考3 注意:MVC是一种搭建项目的思想,不是设计模式. 使用第三方管理控件: 引入CocoaPods的详细步骤:(1)检测有没有引入淘宝镜像gem sou ...