1. 找
1.1. 找字符或串(最后一次)
- lastIndexOf(int ch)
- this.codePointAt(k) == ch
- 以Unicode代码为单位
- lastIndexOf(int ch, int fromIndex)
- lastIndexOf(String str)
- lastIndexOf(String str, int fromIndex)
1.2. 找字符或串(第一次)
- indexOf(String str, int fromIndex)
- indexOf(String str)
- indexOf(int ch, int fromIndex)
- indexOf(int ch)
2. 合并
- join(CharSequence delimiter, CharSequence… elements)
String message = String.join("-", "Java", "is", "cool");
- join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
List<String> strings = new LinkedList<>();
strings.add("Java");strings.add("is");
strings.add("cool");
String message = String.join(" ", strings);
3. 访问
4. 修改
- replaceFirst(String regex, String replacement)
- replaceAll(String regex, String replacement)
- replace(CharSequence target, CharSequence replacement)
- replace(char oldChar, char newChar)
- trim() :删前后空格
- toUpperCase() :所有字母大写
- toLowerCase() :所有字母小写
subSequence() :substring(int beginIndex),substring(int beginIndex, int endIndex)
- 一个参数i类型: 删除前i个字符,返回剩余字符
- 两个参数类型: 返回两参数之间的字符(注意: 后者参数位置处的字符不包含)
- split(String regex) 分隔
5. 返回 数组
5.1. 字符数组
5.2. byte数组
byte[] getBytes(String charsetName)
- 使用命名的字符集将此String编码为字节序列,将结果存储到新的字节数组中。
byte[] getBytes(Charset charset)
- 使用给定的charset将该String编码为一个字节序列,将结果存储到新的字节数组中。
byte[] getBytes()
6. 比较
int compareTo(String anotherString)
- 字符串比较: 根据Unicode值
- 返回差值
this.charAt(k)-anotherString.charAt(k)
- 如果没有它们不同的索引位置,则较短的字符串按字典顺序位于较长的字符串之前。 在这种情况下, compareTo返回字符串长度的差异
compareToIgnoreCase(String str)
- startsWith(String prefix) : 测试此字符串是否以指定的前缀开头
- startsWith(String prefix, int toffset) :测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头。
- endsWith(String suffix)
equals(Object anObject)
equalsIgnoreCase(String anotherString)
- regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- ignoreCase :如果 true , true时忽略大小写。
- toffset 该字符串 toffset的起始偏移量。
- other 字符串参数。
- ooffset 字符串参数 ooffset的起始偏移量。
- len 要比较的字符数。
- regionMatches(int toffset, String other, int ooffset, int len)
7. 值转化
8. 返回Unicode值
- codePointAt(int index)
- codePointBefore(int index)
- codePointCount(int beginIndex, int endIndex)
- 返回此 String指定文本范围内的Unicode代码点数。