时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 字符串方法equals,equalsIgnoreCase,compareTo和regionMatches。
Java 字符串方法equals,equalsIgnoreCase,compareTo和regionMatches。
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
470
查看 / 运行 实例源码
字符串方法equals,equalsIgnoreCase,compareTo和regionMatches。
实例源码:
源代码:
执行
执行中...
public class Main {/* from 时 代 J a v a - nowjava.com*/ public static void main(String[] args) { String s1 = new String("hello"); // s1 is a copy of "hello" String s2 = "this is a test"; String s3 = "Happy Birthday"; String s4 = "happy birthday"; System.out.printf( "s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n\n", s1, s2, s3, s4); // test for equality if (s1.equals("hello")) // true /*来自 时 代 J a v a 公 众 号 - nowjava.com*/ System.out.println("s1 equals \"hello\""); else System.out.println("s1 does not equal \"hello\""); // test for equality with == if (s1 == "hello") // false; they are not the same object System.out.println("s1 is the same object as \"hello\""); else System.out.println("s1 is not the same object as \"hello\""); // test for equality (ignore case) if (s3.equalsIgnoreCase(s4)) // true System.out.printf("%s equals %s with case ignored\n", s3, s4); else System.out.println("s3 does not equal s4"); // test compareTo System.out.printf( "\ns1.compareTo(s2) is %d", s1.compareTo(s2)); System.out.printf( "\ns2.compareTo(s1) is %d", s2.compareTo(s1)); System.out.printf( "\ns1.compareTo(s1) is %d", s1.compareTo(s1)); System.out.printf( "\ns3.compareTo(s4) is %d", s3.compareTo(s4)); System.out.printf( "\ns4.compareTo(s3) is %d\n\n", s4.compareTo(s3)); // test regionMatches (case sensitive) if (s3.regionMatches(0, s4, 0, 5)) System.out.println("First 5 characters of s3 and s4 match"); else System.out.println( /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
s1 = hello
s2 = this is a test
s3 = Happy Birthday
s4 = happy birthday
s1 equals "hello"
s1 is not the same object as "hello"
Happy Birthday equals happy birthday with case ignored
s1.compareTo(s2) is -12
s2.compareTo(s1) is 12
s1.compareTo(s1) is 0
s3.compareTo(s4) is -32
s4.compareTo(s3) is 32
First 5 characters of s3 and s4 do not match
First 5 characters of s3 and s4 match with case ignored
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部