在项目实际开发过程中,只要是项目就一定会存在String类的定义。
1JavaDoc文档简介
在进行开发的过程之中肯定要大量的去使用Java的API文档(JavaDoc),这个文档可以直接通过oracle的在线访问进行查看。
在JDK 1.9之前,所有的Java中的常用类库都会在JVM启动的时候进行全部的加载,这样的话性能会有所下降,在JDK 1.9之后提供有模块化的设计,将一些程序类放在了不同的模块里面。在模块中会包含有大量的程序开发包。
一般文档里的组成:
- 类的完整定义;
- 类相关说明信息;
- 成员属性摘要;
- 构造方法摘要,如果看见有deprecated描述的方法表示不建议使用。
- 方法摘要:左边为返回值,右边为方法名称和相应的参数。
- 方法和成员的详细解释了。
2 字符串与字符
在JDK 1.9一前,所有的String都利用了字符数组实现了包装的处理,所以在String类里面是提供有相应的转换处理方法的。这些方法包含构造方法和普通方法两类。
No. | 名称 | 类型 | 描述 |
---|---|---|---|
1 | public String(char[] value) | 构造 | 将传入的字符数组变为字符串 |
2 | public String(int[] codePoints, int offset, int count) | 构造 | 将部分字符数组变为字符串 |
3 | public char charAt(int index) | 普通 | 获取指定索引位置的字符 |
4 | public char[] toCharArray() | 普通 | 将字符串中的数据以字符数组的形式返回 |
在实际开发之中处理中文的时候往往使用char类型,因为其包含中文数据。
public class StringDemo{
public static void main(String[] args){
char[] ch = new char[]{'1','2','3','4','5'};
String strA = new String(ch);
String strB = new String(ch, 1, 2);
System.out.println(strA);
System.out.println(strB);
}
}
3 字符串与字节
当进行了字符串与字节转换时,其主要目的是为了进行二进制的数据传输或者是进行编码转换。
No. | 名称 | 类型 | 描述 |
---|---|---|---|
1 | public String(byte[] bytes) | 构造 | 将全部字节数组变为字符串 |
2 | public String(byte[] bytes, int offset, int length) | 构造 | 将部分字节数组变为字符串 |
3 | public byte[] getBytes() | 普通 | 将字符串转为字节数组 |
4 | public byte[] getBytes(String charsetName) throws UnsupportedEncodingException | 普通 | 编码转换 |
public class StringDemo{
public static void main(String[] args){
byte[] bytes = new byte[]{'1','2','3','4','5'};
String strA = new String(bytes);
String strB = new String(bytes, 1, 2);
System.out.println(strA);
System.out.println(strB);
}
}
4 字符串比较
字符串比较里面最为常用的方法就是equals方法,但是这个方法需要注意的是其进行大小写区分的。
No. | 名称 | 类型 | 描述 |
---|---|---|---|
1 | public boolean equals(String anObject) | 普通 | 区分大小写的相等判断 |
2 | public boolean equalsIgnoreCase(String anotherString) | 普通 | 不区分大小写的相等判断 |
3 | public int compareTo(String anotherString) | 普通 | 进行字符串大小比较,该方法返回一个int数据,该数据有三种取值:大于0、小于0、等于0 |
4 | public int compareToIgnoreCase(String str) | 普通 | 不区分大小写进行字符串大小比较 |
5 字符串查找
No. | 名称 | 类型 | 描述 |
---|---|---|---|
1 | public boolean contains(CharSequence s) | 普通 | 判断字符串是否存在 |
2 | public int indexOf(String str) | 普通 | 从头查找指定字符串的位置,找不到返回-1,在开发之中利用此形式来进行索引的位置的确定 |
3 | public int indexOf(String str, int fromIndex) | 普通 | 从指定位查找,由前向后 |
4 | public int lastIndexOf(String str) | 普通 | 从末尾查找指定字符串的位置,找不到返回-1 |
5 | public int lastIndexOf(String str, int fromIndex) | 普通 | 从指定位查找,由后向前 |
6 | public boolean startsWith(String prefix) | 普通 | 判断是否以指定字符开头 |
7 | public boolean endsWith(String suffix) | 普通 | 判断是否以指定字符结尾 |
6 字符串替换
No. | 名称 | 类型 | 描述 |
---|---|---|---|
1 | public String replaceAll(String regex, String replacement) | 普通 | 用指定字符替换字符串中的所有指定字符 |
2 | public String replaceFirst(String regex, String replacement) | 普通 | 用指定字符替换字符串中的第一个指定字符 |
7 字符串拆分
No. | 名称 | 类型 | 描述 |
---|---|---|---|
1 | public String[] split(String regex) | 普通 | 根据指定字符拆分字符串 |
2 | public String[] split(String regex, int limit) | 普通 | 根据指定字符拆分字符串,不够拆就不拆了 |
遇见拆不了的情况,这个时候最简单的理解就是使用\进行转义。
8 字符串截取
No. | 名称 | 类型 | 描述 |
---|---|---|---|
1 | public String substring(int beginIndex) | 普通 | 从字符串的指定位置截取子串 |
9 字符串格式化
No. | 名称 | 类型 | 描述 |
---|---|---|---|
1 | public static String format(String format, Object… args) | 普通 | 格式化输出 |
10 其他操作方法
concat() 连接字符串,注意区别+号。
isEmpty() 判断字符串是否为空串,不是null。
length() 返回字符串长度。
trim() 去除字符串首尾空格。
toUpperCase() 字母变为大写。
toLowerCase() 字母变为小写。
首字母变为大写代码:
public class StringDemo{
public static String capitalize(String str){
if( str.isEmpty() || str == null ){
return str;
}
if( str.length() == 1 ){
return str.toUpperCase();
}
return str.substring(0,1).toUpperCase() + str.substring(1);
}
public static void main(String[] args){
System.out.println(StringDemo.capitalize("Hello"));
System.out.println(StringDemo.capitalize("m"));
}
}