首页 技术 正文
技术 2022年11月22日
0 收藏 911 点赞 2,687 浏览 1814 个字

1. String类的判断功能:

boolean  equals (Object  obj )
boolean equalsIgnoreCase (String str )
boolean contains (String str )
boolean startsWith (String str )
boolean endsWith (String str )
boolean isEmpty()

2. 案例:

 package cn.itcast_03; /*
* String类的判断功能:
* boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* 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"; // boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
System.out.println("equals:" + s1.equals(s2));
System.out.println("equals:" + s1.equals(s3));
System.out.println("-----------------------"); // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println("equals:" + s1.equalsIgnoreCase(s2));
System.out.println("equals:" + s1.equalsIgnoreCase(s3));
System.out.println("-----------------------"); // boolean contains(String str):判断大字符串中是否包含小字符串
System.out.println("contains:" + s1.contains("hello"));
System.out.println("contains:" + s1.contains("hw"));
System.out.println("-----------------------"); // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
System.out.println("startsWith:" + s1.startsWith("h"));
System.out.println("startsWith:" + s1.startsWith("hello"));
System.out.println("startsWith:" + s1.startsWith("world"));
System.out.println("-----------------------"); // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩 // boolean isEmpty():判断字符串是否为空。
System.out.println("isEmpty:" + s1.isEmpty()); String s4 = "";
String s5 = null;
System.out.println("isEmpty:" + s4.isEmpty());
// NullPointerException
// s5对象都不存在,所以不能调用方法,空指针异常
System.out.println("isEmpty:" + s5.isEmpty());
}
}

运行结果如下:

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,361
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853