package Day0310.Demo02;
public class StringTest02 {
public static void main(String[] args) {
System.out.println("1.charAt(int index);输出字符串中的某一个字符");
char c = "中国人".charAt(1);
System.out.println(c);
System.out.println("2.int compareTo(String anotherString)用于字符串之间比较大小");
int result = "abc".compareTo("abc");
System.out.println(result);
int result2 = "abcd".compareTo("bcde");
System.out.println(result2);
int result3 = "bcd".compareTo("abcd");
System.out.println(result3);
System.out.println("3.boolean contains(CharSequence s)判断前面的字符串是否包含后面的子字符串");
System.out.println("HelloWorld".contains("Hello"));
System.out.println("abcd".contains("ghij"));
System.out.println("4.boolean endsWith(String suffix)判断当前的字符串是否以某个字符串结尾");
System.out.println("test.txt".endsWith("txt"));
System.out.println("text.exe".endsWith("txt"));
System.out.println("abcdfff".endsWith("fff"));
System.out.println("5.equals(Object anObject)比较两个字符串");
System.out.println("abc".equals("abc"));
System.out.println("6.equalsIgnoreCase(String anotherString)判断两个字符串是否相等,并且同时忽略大小写");
System.out.println("ABc".equalsIgnoreCase("abc"));
System.out.println("7.byte getByte()将字符串对象转换成字符数组");
byte[] bytes = "abcedf".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("8.int indexOf(String str)判断某个子字符串在当前字符串中第一次出现位置的索引(下标);");
System.out.println("oraclejavac++.netc#phppythonjavaoraclec++".indexOf("java"));
System.out.println("9.boolean isEmpty()判断某个字符串是否为\"空字符串\"。底层源代码调用的是字符串的length()方法");
String s = "a";
System.out.println(s.isEmpty());
System.out.println("10.面试题:判断数组长度和判断字符串长度不一样,\n判断字符串长度是length()方法,判断数组长度是length属性");
System.out.println("abc".length());
System.out.println("11.int lastIndexOf(String str)判断某个子字符串在当前字符串中最后一次出现位置的索引(下标)");
System.out.println("abcdefggfedcba".lastIndexOf("c"));
System.out.println("12.String replace(CharSquence target,CharSquence replacement)\n替换字符串");
String newString = "http://www.baidu.com".replace("http://","https://");
System.out.println(newString);
System.out.println("13.String[] split(String regex)\n拆分字符串");
String[] ymd = "1980-10-11".split("-");
for (int i = 0; i < ymd.length; i++) {
System.out.println(ymd[i]);
}
System.out.println("14.starWith(String str)\n判断某个字符串是否以某个子字符串开头");
System.out.println("http://www.baidu.com".startsWith("http://"));
System.out.println("http://www.baidu.com".startsWith("https://"));
System.out.println("15.String substring(int beginIndex)\n参数是起始下标,截取字符串");
System.out.println("http://wwww.baidu.com".substring(7));
System.out.println("16.String substring(int beginIndex,int endIndex)\nbeginIndex起始位置\nendIndex结束位置(不包含)");
System.out.println("http://www.baidu.com".substring(7,10));
System.out.println("17.char[] toCharArray()\n将字符串转换成char数组");
char[] chars = "魅族18".toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
System.out.println("18.String toLowerCase()\n转换成小写");
System.out.println("aABCDEFG".toLowerCase());
System.out.println("19.String toUpperCase()\n转换大写");
System.out.println("abcdefgh".toUpperCase());
System.out.println("20.String trim()去掉字符串前后空白");
System.out.println(" Hello World ".trim());
System.out.println("21.String中只有一个方法是静态方法,不需要new对象\n" +
"这个方法叫valueOf\n作用:将非字符串转换成字符串");
String s1 = String.valueOf(new Customer());
System.out.println(s1);
}
}
class Customer{
@Override
public String toString() {
return "wo shi VIP";
}
}