day12Java-常用对象String03-成员方法(判断功能)03
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了day12Java-常用对象String03-成员方法(判断功能)03,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2634字,纯文字阅读大概需要4分钟。
内容图文
常用对象String03-成员方法(判断功能)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;
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
public class StringDemo {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "helloworld";
System.out.println("equals()--"+s1.equals(s2));
System.out.println("equals()--"+s1.equals(s3));
}
}
结果:
equals()--true
equals()--true
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
public class StringDemo {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HELLOWORLD";
System.out.println("equalsIgnoreCase()--"+s1.equalsIgnoreCase(s2));
System.out.println("equalsIgnoreCase()--"+s1.equalsIgnoreCase(s3));
}
}
结果:
equalsIgnoreCase()--true
equalsIgnoreCase()--true
boolean contains(String str):判断大字符串中是否包含小字符串
public class StringDemo {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "helloworld";
System.out.println("contains()--"+s1.contains("llow"));
System.out.println("contains()--"+s1.contains("hd"));
}
}
结果:
contains()--true
contains()--false
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
public class StringDemo {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "helloworld";
System.out.println("startsWith()--"+s1.startsWith("h"));
System.out.println("startsWith()--"+s1.startsWith("H"));
}
}
结果:
startsWith()--true
startsWith()--false
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
public class StringDemo {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "helloworld";
System.out.println("endsWith()--"+s1.endsWith("d"));
System.out.println("endsWith()--"+s1.endsWith("D"));
}
}
结果:
endsWith()--true
endsWith()--false
boolean isEmpty():判断字符串是否为空
public class StringDemo {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "";
String s3 = null;
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
System.out.println(s3.isEmpty());//空指针异常
}
}
结果
false
true
Exception in thread "main" java.lang.NullPointerException
at com.ginger.demo01.StringDemo.main(StringDemo.java:12)
内容总结
以上是互联网集市为您收集整理的day12Java-常用对象String03-成员方法(判断功能)03全部内容,希望文章能够帮你解决day12Java-常用对象String03-成员方法(判断功能)03所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。