String类
创建字符串对象
1.String str1 = “哈哈哈哈”;
2.String str2 = new String(“呵呵呵”);
常用方法
判断功能
boolean equals(Object obj) 判断值是否相等
boolean equalsIgnoreCase(String str) 不区分大小写判断
boolean contains(String str) 是否包含
boolean startsWith(String str) 以。。开始
boolean endsWith(String str) 以。。结束
boolean isEmpty() 是否为空
//判断功能
System.out.println(str1 .equals(str2));//false
System.out.println(str1 .equals("hahahah"));//true
System.out.println("abcd" .equalsIgnoreCase("AbcD"));//true
System.out.println("hello world" .contains("hello"));//true
System.out.println("hello world" .contains("aaa"));//false
System.out.println("hello world" .startsWith("hello"));//true
System.out.println("hello world" .endsWith("world"));//true
System.out.println("hello world" .isEmpty());//false
System.out.println("----------------------------------");
获取功能
int length() 获取长度
char charAt(int index) 下标为index的位置的字符
int indexOf(int ch) 第一次出现字符ch的下标
int indexOf(String str) 字符串str首次出现的首字母对应的下标
int indexOf(int ch, int fromIndex) 从。。开始 字符第一次出现的下标
int indexOf(String str, int fromIndex) 从。。开始字符串第一次出现的下标
String substring(int start) 从。。开始的字符串
String substring(int start, int end) 从。。到。。(不包括)结束的字符串
//获取功能
//length() -------- 长度 字符的个数
System.out.println("hello world" .length());//11
//charAt(int index) ----返回指定下标的字符 下标从零开始
System.out.println("hello world" .charAt(0));//'h'
//indexOf(int ch) ----返回字符首次出现的索引(下标)
System.out.println("hello world" .indexOf('e'));//1
//indexOf(String str) ----返回字符串首次出现的索引(下标)
System.out.println("hello world" .indexOf("ll"));//2
//indexOf(int ch, int formindex) ----返回从index下标开始字符首次出现的索引(下标)
System.out.println("hello world" .indexOf('l', 5));//9
//substring(int start,int end) -----返回从下标几到几的子字符串
System.out.println("hello world" .substring(2, 9));//"llo wor"
//Strign ---> byte[]
System.out.println(Arrays.toString("hello" .getBytes()));
//char ---> String
System.out.println(String.valueOf(new char[]{'h','e','l','l','o'}));
System.out.println("-------------------------------------");
//trim() 去除首尾的空格
String s1 = " 1223 ";
String s2 = s1.trim();
System.out.println(s1);
System.out.println(s2);
System.out.println(s1.length());
System.out.println(s2.length());