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 ...
随机推荐
- Fix "Unable to lock the administration directory (/var/lib/dpkg/)" in Ubuntu
While using the apt-get command or the relatively new APT package management tool in Ubuntu Linux or ...
- 查看android-support-v4.jar引出的问题
1.前面博文里也写过如何关联android-support-v4.jar的源码 今天新项目用上述方法的时候,竟然不成功..来回反复试了很长时间,最后发现 新建项目,会自动引用一个类库(自动新建的..) ...
- java8--Mysql数据库与JDBC编程(java疯狂讲义3复习笔记)
JDBC:java database connectivity 数据库基本命令: 启动 show databases; create database [if not exists] 数据库名; do ...
- 6.游戏特别离不开脚本(3)-JS脚本操作java(2)(直接解析JS公式,并非完整JS文件或者函数)
在游戏中可以考虑数据由javabean保存,逻辑方法由JS提供. public class Bean4JS { private int id; private String name; private ...
- CSS 对齐方式
居中设置 Center Align - Using margin Setting the width of a block-level element will prevent it from str ...
- file结构体中private_data指针的疑惑【转】
本文转载自:http://www.cnblogs.com/pengdonglin137/p/3328984.html hi all and barry, 最近在学习字符设备驱动,不太明白private ...
- The input stream is not a valid binary format.
The input stream is not a valid binary format. The starting contents (in bytes) are: 53-79-73-74-65- ...
- c的free注意事项和c++的简洁(析构大哥)
#include <iostream> using namespace std; // ////c语言版本 //struct stu //{ // char *name; // int a ...
- asp.net MVC4 学习(一)
asp.net MVC 回顾 Html.ActionLink http://www.cnblogs.com/jiagoushi/p/3905828.html 选择基本模板,视图引擎 选择Razor A ...
- js 获取url的request参数
方法1: function getRequest(strParame) { var args = new Object(); var query = location.search.substrin ...