String 和 char [] 之间常用
| 方法 | 描述 |
|---|---|
public String(char[] value) | 把传入的字符数组变为字符串 |
public String(char[] value, int offset, int count) | offset 表示开始的位置,count 表示个数 |
public char charAt(int index) | 获取在 index 处的字符 |
String 与 byte [] 之间常用
| 方法 | 描述 |
|---|---|
public String (byte[] bytes) | 把传入的 byte [] 转换为 String |
public String(byte[], int offset, int length) | 将部分字符变成 String |
public byte[] getBytes() | 获取 String 的 byte [] |
public byte[] getBytes(String charsetName) thros (UnsupportedEncodingException) | 编码转换 |
String 的比较
| 方法 | 描述 |
|---|---|
public boolean equals(String anObject) | 是否内容一致的判断(区分大小写) |
public boolean equalsIgnoreCase(String anotherString) | 是否内容一致的判断(不区分大小写) |
public int compareTo(String anotherString) | 大小比较(区分大小写) |
public int compareToIgnoreCase(String str) | 大小比较(不区分大小写) |
字符串查找
| 方法 | 描述 |
|---|---|
public boolean contains(String s) | 判断子字符串是否存在 |
public int indexOf(String str) | 从头开始查找 str 字符串的位置 |
public int indexOf(String str, int fromIndex) | 从 fromIndex 位置开始查找 |
public int lastIndexOf(String str) | 从后往前查找 |
public int lastIndexOf(String str, int fromIndex) | 从 fromIndex 位置开始从后向前查找 |
public boolean startsWith(String prefix) | 判断是否以 prefix 开头 |
public boolean startsWith(String prefix, int offset) | 从 offset 开始判断 |
public boolean endsWith(String suffix) | 判断是否结尾是 suffix |
字符串替换
| 方法 | 描述 |
|---|---|
public String replaceAll(String regex, String replacement) | 全部替换(regex 是要替换的, replacement 是替换成的) |
public String replaceFirst(String regex, String replacement) | 替换首个 |
字符串拆分
| 方法 | 描述 |
|---|---|
public String[] split(String regex) | 以 regex 为准开始拆分 |
public String[] split(String regex, int limit) | limit 为限制的拆分数 |
字符串截取
| 方法 | 描述 |
|---|---|
public String substring(int beginIndex) | 从 beginIndex 开始截取到结尾 |
public String substring(int beginIndex, int endIndex) | 截取范围 |
字符串其他常用
| 方法 | 描述 |
|---|---|
public String concat(String str) | 用于连接 |
public String intern() | 把字符串入池 |
public boolean isEmpty() | 判断字符串是否是 “”,并非 null |
public int length() | 长度 |
public String trim() | 去掉两边的空格(中间的不能去掉) |