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
}
}
String的用法——判断功能的更多相关文章
- Java基础知识强化32:String类之String类的判断功能
1. String类的判断功能: boolean equals (Object obj ) boolean equalsIgnoreCase (String str ) boolean contain ...
- 字符串类String类的判断功能
StringDemo.java /* * Object:是类层级结构中的根类,所有的类都直接或间接的继承自该类. * 如果一个方法的形式参数是Object,那么这里我们就可以传递它的任意的子类对象. ...
- String类的判断功能
/* * Object:是类层级结构中的根类,所有的类都直接或间接的继承自该类. * 如果一个方法的形式参数是Object,那么这里我们就可以传递它的任意的子类对象. * * String类的判断功能 ...
- String的用法——转换功能
package cn.itcast_05; /* String类的转换功能: byte[] getByte():把字符串转换成字节数组 复习: public String(byte[] bytes): ...
- String的用法——其他功能
package cn.itcast_06; /* String类的其他功能: 替换功能: String replace(char old,char new) String replace(String ...
- String的用法——获取功能
package cn.itcast_04; /* String类获取功能 int length():获取字符的长度 char charAt(int index):获取指定索引位置的字符 int ind ...
- String 类的其他功能
12.01_常见对象(Scanner的概述和方法介绍)(掌握) A:Scanner的概述 B:Scanner的构造方法 Scanner(InputStream source) System.in C: ...
- test命令用法。功能:检查文件和比较值
test命令用法.功能:检查文件和比较值 1)判断表达式 if test (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2 两个表达 ...
- Java基础知识强化72:正则表达式之判断功能(手机号码判断 和 校验邮箱)
1. 判断功能: 使用了String类的matches方法,如下: public boolean matches(String regex): 2. 判断手机号码的案例: package cn.it ...
随机推荐
- Blocks实现代理传值
一.RootViewController: #import "RootViewController.h" #import "SecondViewController.h& ...
- Windows7 配置匿名Samba文件共享
1.环境 系统:Windows 7 SP1 IP:192.168.118.151 2.配置 计算机|管理|本地用户和组|用户|Guest-->去掉账户已禁用 cmd|gpedit.msc|本地计 ...
- RK平台LCD调试说明【转】
本文转载自:http://blog.csdn.net/u014770862/article/details/76274951?locationNum=2&fps=1 RK平台LCD调试说明 原 ...
- YTU 1075: Time
1075: Time 时间限制: 1 Sec 内存限制: 128 MB 提交: 7 解决: 7 [提交][状态][讨论版] 题目描述 Digital clock use 4 digits to e ...
- MARGIN-BEFORE MARGIN-AFTER MARGIN-START MARGIN-END
总的来说:这是CSS3.0的对于文章段P容器的定义方法语句!display:block这个样式,只定义了P容器为一个块;后面四句是CSS3中的样式定义方法:-webkit-margin-before: ...
- bzoj 5017 炸弹
题目大意: 直线上有n个炸弹有坐标x和半径r 当一个炸弹被引爆时 若有炸弹的坐标在该炸弹坐标+-r范围内则另一个炸弹也被引爆 求先引爆每一个炸弹最终会引爆多少炸弹 思路: 可以想到n平方连边然后tar ...
- Code Project精彩系列(转)
Code Project精彩系列(转) Code Project精彩系列(转) Applications Crafting a C# forms Editor From scratch htt ...
- openpyxl操作excel
[转] openpyxl库可以读写xlsx格式的文件,对于xls旧格式的文件只能用xlrd读,xlwt写来完成了. python有很多模块都是用来操作excel的,比如xlrd,xlwt,pyExce ...
- ChartCtrl源码剖析之——CChartAxis类
CChartAxis类用来绘制波形控件的坐标轴,这个源码相对较复杂,当初阅读的时候耗费了不少精力来理解源码中的一些实现细节. CChartAxis类的头文件. #if !defined(AFX_CHA ...
- INT_PTR数据类型
A signed integer type for pointer precision. Use when casting a pointer to an integer to perform poi ...