简介
本文介绍Java中的String的常见方法。
索引位置
| 方法 | 作用 | 
| char charAt(int index) | 方法返回指定索引位置的char值。索引范围为0 ~ length()-1. | 
| int indexOf(int ch,int fromIndex)) | 从fromIndex出开始从左往右找,第一次出现字符ch所对应的索引 | 
| int indexOf(int ch,int fromIndex) | 从fromIndex出开始从左往右找,第一次出现字符ch所对应的索引 | 
| int indexOf(String str,int fromIndex) | 指定字符串在此字符串中第一次出现处的索引 | 
| int indexOf(int ch) | 指定字符在此字符串中第一次出现处的索引 | 
匹配
| 方法 | 作用 | 
| boolean matches(String regex) | 字符串是否符合regex格式 | 
| boolean isEmpty() | 字符串是否为空。 | 
| boolean equals(Object obj) | 将此字符串的内容与指定的对象比较,区分大小写。 | 
| boolean equalsIgnoreCase(String str) | 将此 String 与另一个 String 比较,忽略大小写。 | 
| boolean contains(String str) | 判断字符串中是否包含方法传入的字符串。 | 
| boolean startsWith(String str) | 判断字符串是否以某个指定的字符串开头。 | 
| boolean endsWith(String str) | 判断字符串是否以某个指定的字符串结尾。 | 
| int compareTo(String str) | 按字典顺序比较两个字符串。 | 
| int compareToIgnoreCase(String str) | 按字典顺序比较两个字符串,忽略大小写。 | 
数组
| 方法 | 作用 | 
| byte[] getBytes() | 将字符串转化为字节数组。 | 
| char[] toCharArray() | 将字符串转化为字符数组。 | 
| public String[] split(String regex) | 分隔字符串成String数组。 | 
| public String[] split(String regex, int limit) | 分隔字符串成String数组。limit表示分成几部分,例如: String string = "ab,cd,ef,gh,ij"; 结果: | 
| static String join(CharSequence delimiter, CharSequence... elements) | 将第2个参数代表的字符串数组拼接为一个字符串,并用第1个参数隔开 | 
| static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) | 将第2个参数代表的字符串数组拼接为一个字符串,并用第1个参数隔开 | 
子字符串
| 方法 | 作用 | 
| String substring(int beginIndex) | 返回指定开始位置直到最后子字符串。 | 
| String substring(int beginIndex, int endIndex) | 返回指定开始与结束位置的子字符串。 | 
| CharSequence subSequence(int beginIndex, int endIndex) | 返回指定开始与结束位置的子字符串序列。 | 
修改
| 方法 | 作用 | 
| static String valueOf(char[] chs) | 返回 char 数组参数的字符串表示形式。 | 
| static String valueOf(int i) | 返回 int 参数的字符串表示形式。 | 
| String toLowerCase() | 将此 String 中的所有字符都转换为小写。 | 
| String toUpperCase() | 将此 String 中的所有字符都转换为大写。 | 
| String concat(String str) | 将指定字符串连接到此字符串的结尾。 | 
| String replace(char old,char new) | 替换字符。 | 
| String replace(String old,String new) | 替换字符串。(会替换所有匹配的字符串,并不是只替换第一个) | 
| String replaceAll(String regex,String replacement) | 按正则表达式替换字符串。 | 
| String replaceFirst(String regex, String replacement) | 按正则表达式替换第一次匹配的字符串 | 
| String trim() | 去除头尾ASCII码小于等于空格的东西(退格、tab、回车、空格等) | 
其他
| 方法 | 作用 | 
| int length() | 字符串的长度 | 










