String的判断功能
/*
- 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):判断字符串是否以某个指定的字符串结尾这个自己玩
System.out.println("判断字符串结尾是否是以摸个指定的结尾结尾");
System.out.println("endsWith:" + s1.endsWith("d"));
System.out.println("endsWith:" + s1.endsWith("word"));
System.out.println("endsWith:" + s1.endsWith("orld"));
System.out.println("-----------------------");
// 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());
}
}
2. String的获取功能
/*
-
String类的获取功能
-
int length():获取字符串的长度。
-
char charAt(int index):获取指定索引位置的字符
-
int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
-
为什么这里是int类型,而不是char类型?
-
原因是:'a'和97其实都可以代表'a'
-
int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
-
int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
-
int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
-
String substring(int start):从指定位置开始截取字符串,默认到末尾。
-
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
*/
public class StringDemo {
public static void main(String[] args) {
// 定义一个字符串对象
String s = “helloworldllhe”;// int length():获取字符串的长度。 System.out.println("s.length:" + s.length()); System.out.println("----------------------"); // char charAt(int index):获取指定索引位置的字符 System.out.println("charAt:" + s.charAt(0)+s.hashCode()); System.out.println("----------------------"); // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。 System.out.println("indexOf:" + s.indexOf('l')); System.out.println("----------------------"); // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。 System.out.println("indexOf:" + s.indexOf("owo")); System.out.println("----------------------"); // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。 System.out.println("indexOf:" + s.indexOf('l', 4)); System.out.println("indexOf:" + s.indexOf('k', 4)); // -1 System.out.println("indexOf:" + s.indexOf('l', 40)); // -1 System.out.println("----------------------"); // 自己练习:int indexOf(String str,intfromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。 System.out.println("indexOf:" + s.indexOf("ll", 4)); System.out.println("indexOf:" + s.indexOf("he", 4)); // -1 System.out.println("indexOf:" + s.indexOf("ll", 40)); // -1 System.out.println("----------------------"); // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引 System.out.println("substring:" + s.substring(5)); System.out.println("substring:" + s.substring(0)); System.out.println("----------------------"); // String substring(int start,int); // end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引 System.out.println("substring:" + s.substring(3, 8)); System.out.println("substring:" + s.substring(0, s.length()));
}
}
String的转换功能
/*
-
String的转换功能:
-
byte[] getBytes():把字符串转换为字节数组。
-
char[] toCharArray():把字符串转换为字符数组。
-
static String valueOf(char[] chs):把字符数组转成字符串。
-
static String valueOf(int i):把int类型的数据转成字符串。
-
注意:String类的valueOf方法可以把任意类型的数据转成字符串。
-
String toLowerCase():把字符串转成小写。
-
String toUpperCase():把字符串转成大写。
-
String concat(String str):把字符串拼接。
*/
public class StringDemo {
public static void main(String[] args) {
// 定义一个字符串对象
String s = “JavaSE”;// byte[] getBytes():把字符串转换为字节数组。 byte[] bys = s.getBytes(); for (int x = 0; x < bys.length; x++) { System.out.println(bys[x]); } System.out.println("----------------"); // char[] toCharArray():把字符串转换为字符数组。 char[] chs = s.toCharArray(); for (int x = 0; x < chs.length; x++) { System.out.println(chs[x]); } System.out.println("----------------"); System.out.println(" static String valueOf(char[] chs):把字符数组转成字符串。"); String ss = String.valueOf(chs); System.out.println(ss); System.out.println("----------------"); // static String valueOf(int i):把int类型的数据转成字符串。 int i = 100; String sss = String.valueOf(i); System.out.println(sss); System.out.println("----------------"); // String toLowerCase():把字符串转成小写。 System.out.println("toLowerCase:" + s.toLowerCase()); System.out.println("s:" + s); // System.out.println("----------------"); // String toUpperCase():把字符串转成大写。 System.out.println("toUpperCase:" + s.toUpperCase()); System.out.println("----------------"); // String concat(String str):把字符串拼接。 String s1 = "hello"; String s2 = "world"; String s3 = s1 + s2; String s4 = s1.concat(s2); System.out.println("s3:"+s3); System.out.println("s4:"+s4);
}
}