1 是否包含字串
boolean contains(String str) - For example:
- String str1 = "abcdefgcdefdsadsdef";
- String str2 = "abc";
- String str3 = "dc";
- System.out.println(str1.contains(str2));//true
- System.out.println(str1.contains(str3));//false
复制代码 特殊之处:indexOf(str):可以索引str第一次出现位置,如果返回-1.表示该str不在字符串中存在。所以,也可以用于对指定判断是否包含。if(str.indexOf("aa")!=-1)而且该方法即可以判断,有可以获取出现的位置。
- For example:
- String str1 = "abcdefgcdefdsadsdef";
- String str2 = "abc";
- String str3 = "dc";
- System.out.println(str1.indexOf(str2));//0
- System.out.println(str1.indexOf(str3));//-1
复制代码 2 是否有内容
boolean isEmpty();原理就是判断长度是否为0. - For example,
- String str1 = "abcdefgcdefdsadsdef";
- String str2 = "";
- System.out.println(str1.isEmpty());//false
- System.out.println(str2.isEmpty());//true
复制代码 3 是否以指定前缀开头
boolean startsWith(String str) - For example,
- String str1 = "abcdefgcdefdsadsdef";
- System.out.println(str1.startsWith("abc"));//true
- System.out.println(str1.startsWith("abd"));//false
复制代码 4 是否以指定后缀结尾boolean endsWith(String str) - For example.
- String str1 = "abcdefgcdefdsadsdef";
- System.out.println(str1.endsWith("def"));//true
- System.out.println(str1.endsWith("sd"));//false
复制代码 5 字符串内容是否相等
boolean equals(String str) - For example,
- String str1 = "abcdefgcdefdsadsdef";
- String str2 = new String("abcdefgcdefdsadsdef");
- System.out.println(str1.equals(str2));//true
- System.out.println(str1==str2);//false
复制代码
6 忽略大小写字符串内容是否相等boolean equalsIgnoreCase(String str) - For example,
- String str1 = "abcdefgcdefdsadsdef";
- String str2 = new String("ABCDEFGCDEFDSADSDEF");
- System.out.println(str1.equals(str2));//false
- System.out.println(str1.equalsIgnoreCase(str2));//true
复制代码
|