| 方法语句 | 方法类型 | 功能 |
|---|---|---|
| public String(char[ ] value) | 构造 | 将传入的字符数组转化为字符串 |
| public String(char[] value, int offset, int count) | 构造 | 将部分字符数组转化为字符串 |
| public char charAt(int index) | 普通 | 获取指定索引位置的字符 |
| public char[] toCharArray() | 普通 | 将字符串转化为字符数组 |
例:
String str = "helloworld"; //定义一个字符串
char a = str.charAt(4); //charAt方法,将str字符串的第四个字母赋值给a
System.out.println("a= "+a); //a的值为o
char[] cha = str.toCharArray(); //将字符串转化为字符数组
for (int i=0;i< cha.length;i++){
cha[i]-=32; //实现字符数组的每一项变为大写
}
String newstr =new String(cha); //将字符数组转化为字符串
String newstr1 = new String(cha,0,3);//将字符数组下标从0到3的元素转化为字符串
System.out.println(newstr); //结果为"HELLOWORLD"
System.out.println(newstr1); //结果为"HEL"
二,字符串与字节数组相关方法
字符串转化为字节数组,主要为了进行二进制的数据传输,或者是进行编码转换。
| No. | 方法名称 | 类型 | 功能 |
|---|---|---|---|
| 01 | public String(byte[] bytes) | 构造 | 将全部的字节数组变为字符串 |
| 02 | public String(byte[] bytes,int offset,int lenth) | 构造 | 将部分字节数组变为字符串 |
| 03 | public byte[] getBytes() | 普通 | 将字符串转为字符数组 |
| 04 | public byte[] getBytes(String charsetName)throws UnsupportedEncodingException | 普通 | 编码转换 |
例与字符数组相同,但字节所表示范围在-128到127之间。
三,字符串比较| No. | 方法名称 | 类型 | 功能 |
|---|---|---|---|
| 01 | public boolean equals(String anObject) | 普通 | 区分大小写比较是否相同 |
| 02 | public boolean equalsIgnoreCase(String anotherString) | 普通 | 不区分大小写比较是否相同 |
| 03 | public int compareTo(String anotherString) | 普通 | 进行字符串大小比较,并返回值,区分大小写 |
| 04 | public int compareToIgnoreCase(String str) | 普通 | 进行字符串大小比较,不区分大小写 |
例:
String str1 = "helloworld";
String str2 = "Helloworld";
//定义了两个字符串
System.out.println(str1.equals(str2));//输出结果为false
System.out.println(str1.equalsIgnoreCase(str2));//输出结果为true
System.out.println(str1.compareTo(str2)); //输出结果为32
System.out.println(str1.compareToIgnoreCase(str2));//输出结果为0
四,字符串查找
| No. | 方法名称 | 类型 | 功能 |
|---|---|---|---|
| 01 | public boolean contains(String s) | 普通 | 判断子字符串是否存在 |
| 02 | public int indexOf(String str) | 普通 | 从头查找指定字符串位置 |
| 03 | public int indexOf(String str,int fromIndex) | 普通 | 从指定位置查找指定字符串位置 |
| 04 | public int lastIndexOf(String str) | 普通 | 从后向前查找指定字符串的位置 |
| 05 | public int lastIndexOf(String str,int fromIndex) | 普通 | 从指定位置开始由后往前查找指定的字符串的位置 |
| 06 | public boolean startsWith(String prefix) | 普通 | 判断是否以指定的字符串开头 |
| 07 | public boolean startsWith(String prefix,int offset) | 普通 | 由指定位置判断是否以指定字符串开头 |
| 08 | public boolean endsWith(Stringt suffix) | 普通 | 判断是否以指定的字符串结尾 |
例:
String str1 = "helloworld"; //定义一个字符串
String str2 = "hello";
String str3 = "world";
System.out.println(str1.contains(str2)); //结果为true
System.out.println(str1.indexOf(str3)); //结果为5
System.out.println(str1.indexOf(str3,2)); //结果为5
System.out.println(str1.lastIndexOf(str3)); //结果为5
System.out.println(str1.lastIndexOf(str3,3)); //结果为-1
System.out.println(str1.startsWith("hel")); //结果为true
System.out.println(str1.startsWith("world",5)); //结果为true
System.out.println(str1.endsWith("world")); //结果为true
五,字符串替换
| No. | 方法名称 | 类型 | 功能 |
|---|---|---|---|
| 01 | public String replaceAll(String regex,String replacement) | 普通 | 全部替换 |
| 02 | public String replaceFirst(String regex,String replacement) | 普通 | 替换首个 |
例:
String str1 = "helloworld"; //定义一个字符串
System.out.println(str1.replaceAll("l","_")); //结果为he__owor_d
System.out.println(str1.replaceFirst("l","_")); //结果为he_lloworld
六,字符串的拆分
| No. | 方法名称 | 类型 | 功能 |
|---|---|---|---|
| 01 | public String[] split(String regex) | 普通 | 按照指定的字符串全部拆分 |
| 02 | public String[] split(String regex,int limit) | 普通 | 按照指定的字符串拆分为指定的部分 |
例:
String str1 = "helloworld";
String[] str2 = str1.split("w");
for (int x=0;x< str2.length;x++){
System.out.println(str2[x]);
} //结果为hello,orld
String[] str3 = str1.split("l",2);
for (int y=0;y< str3.length;y++){
System.out.println(str3[y]);
} //结果为he,loworld
七,字符串的截取
| No. | 方法名称 | 类型 | 功能 |
|---|---|---|---|
| 01 | public String substring(int beginIndex) | 普通 | 从指定位置截取到结尾 |
| 02 | public String substring(int beginIndex,int endIndex) | 普通 | 截取指定索引范围内的字符串 |
例:显然
八,其他操作| No. | 方法名称 | 类型 | 功能 |
|---|---|---|---|
| 01 | public String concat(String str) | 普通 | 字符串的连接 |
| 02 | public String intern() | 普通 | 字符串入池 |
| 03 | public boolean isEmpty() | 普通 | 判断是否是空字符串 |
| 04 | public int length() | 普通 | 计算字符串的长度 |
| 05 | pubic String trim() | 普通 | 去除左右的空格信息 |
| 06 | public String toUpperCase() | 普通 | 转大写 |
| 07 | public String toLowerCase() | 普通 | 转小写 |
学了一段时间Java,这些是String类中常用的方法,留着给自己忘记时看啦。
喜欢记得收藏,点赞!



