String的常用方法

函数 说明
int length() 返回字符串的长度
char charAt(int index) 返回指定索引处的字符
boolean isEmpty() 判断字符串是否为空
String toLowerCase() 将字符串中的所有字符转换为小写
String toUpperCase() 将字符串中的所有字符转换为大写
String trim() 返回字符串的副本,去掉前导空白和尾部空白,中间的空白不会被去掉
boolean equals(Object obj) 比较字符串的内容是否相同
boolean equalsIgnoreCase(String anotherString) 忽略大小写,比较字符串的内容是否相同
String concat(String str) 将指定字符串连接到此字符串的结尾,等价于用“+”
String substring(int beginIndex) 返回从beginIndex到末尾的子字符串
String substring(int beginIndex, int endIndex) 返回从beginIndex到endIndex前一位的子字符串,不包括endIndex
boolean endsWith 判断字符串是否以指定的后缀结束
boolean startsWith(String prefix) 判断字符串是否以指定的前缀开始
boolean startsWith(String prefix, int toffset) 判断字符串在指定索引开始的子字符串是否以指定前缀开始
boolean contains(CharSequence s) 判断当前字符串中是否包含指定的字符串
int indexOf(String str) 返回指定子字符串在当前字符串中第一次出现处的索引
int indexOf(String str, int fromIndex) 返回从指定的索引后,指定子字符串在当前字符串中第一次出现处的索引
int lastIndexOf(String str) 返回指定子字符串在当前字符串中最后一次出现处的索引
int lastIndexOf(String str, int fromIndex) 返回从指定的索引后,指定子字符串在当前字符串中最后一次出现处的索引
注:indexOf和lastIndexOf方法如果未查找到指定子字符串时,返回值都为-1。
String[] split(String regex) 根据指定的符号拆分当前字符串,然后返回一个String数组
posted @ 2022-09-18 10:51  DevourZuan  阅读(20)  评论(0编辑  收藏  举报