No. | 方法名 | 类型 | 描述 |
1 | public String(char[ ] chars) | 构造 | 将字符数组变成字符串 |
2 | public char charAt(index) | 普通 | 返回指定所应处的字符 |
3 | public String(byte[ ] bytes) | 构造 | 将字节数组变成字符串 |
4 | public char[ ] toCharArray( ) | 普通 | 将字符串变成字符数组 |
5 | public byte[ ] getBytes( ) | 普通 | 将字符串变成字节数组 |
6 | public boolean equals( String str) | 普通 | 比较两个字符串的内容,区分大小写 |
7 | public boolean equalslgnoreCase | 普通 | 比较两个字符串的内容,不区分大小写 |
8 | public int compareTo(String anotherString) | 普通 | 判断两个字符串的大小(按照字符 编码比较),此方法的返回值有以下 3种: = 0:表示要表示的两个字符串内容相等; > 0:表示大于; < 0:表示小于; |
9 | public int indexOf(Sting str) | 普通 | 由前向后查找指定字符串的位置,如果查找到了则返回(第一个字母)位置素引,如果找不到返回-1 |
10 | public boolean startsWith(String prefix) | 普通 | 判断是否以指定的字符串开头 |
11 | public boolean endsWith(String suffix) | 普通 | 判断是否以指定的字符串结尾 |
12 | public String replaceAll(String regex, String replacement) | 普通 | 用新的内容替换所有旧内容 |
13 | public String substring(int beginlndex) | 普通 | 从指定索引截取到结尾 |
14 | public String[ ] split(String regex) | 普通 | 按照指定的字符串进行全部拆分 |
15 | public String toLowerCase() | 普通 | 转小写 |
16 | public String toUpperCase() | 普通 | 转大写 |
17 | public String trim() | 普通 | 去掉字符串中左右两边的空格, 中 间空格保留 |
18 | public int length() | 普通 | 取得字符串长度 |
19 | public boolean isEmpty() | 普通 | 判断是否是空字符串(不是 null, 而是””,长度 0) |
20 | public boolean contains(String s) | 普通 | 判断指定的内容是否存在 |
21 | public String substring(int beginlndex, int endlndex) | 普通 | 从指定位置截取部分字符串,不包括endlndex索引位置 |
需求:切割字符串,将首字母变成大写
public class StringMethod {
public static void main(String[] args) {
String str = "may cattle marry king jason switch";
//按" ",切割字符串
String[] strings = str.split(" ");
//循环遍历取出字符串数组中的元素
for (int i = 0; i < strings.length; i++) {
//取得首字母并转成大写
String temp = (strings[i].charAt(0)+"").toUpperCase();
//拼接转换后的字符串
strings[i] = temp+strings[i].substring(1,strings[i].length());
}
//输出字符串
for (int i = 0; i < strings.length ; i++) {
System.out.println(strings[i]);
}
}
}
运行结果: May
Cattle
Marry
King
Jason
Switch