String的特点
- 字符串不能被继承,被关键字final修饰,因String类是一个最终类。查看源码可知:
public final class String{}
- 字符串不可变性,一旦定义不可修改(长度和值)都不能变,因String类底层维护了一个用关键字final的数组char[],也可以说字符串就是一个常量。参考源码:
private final char value[];
String创建对象
- String(char[] value) 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
char[] c = new char[]{'a','b','c'}
String str = new String(c);
- String.valueOf(char[] data) 返回 char 数组参数的字符串表示形式。
public static String valueOf(char data[]) {
return new String(data);
}
- 直接给字符串类型的变量赋值,此方法高效,因这种方式的创建的字符串放在之间的缓存区中,下次使用可以直接调用
String str1 = "hello";
String str2 = "hello";
// 下面的两次输出,证明str1和str2的内存地址相同,即为同一个字符串对象
System.out.println(str1 == str2); //true
System.out.println(str1.equals(str2)); // true
常用方法
// char charAt(int index) 返回指定索引处的 char 值。
System.out.println( "apple".charAt(1) );//p
// string concat(CharSequence cs) 将指定字符串连接到此字符串的结尾
System.out.println( "app".concat("le") );// apple
// boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。
System.out.println( "apple".contains("app") ); // true 包含指定字符串返回true
System.out.println( "apple".contains("ab") ); //false 不包含指定字符串返回false
// boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
System.out.println( "apple".endsWith("le") );// true 以指定元素结束返回true
System.out.println( "apple".endsWith("lp") );// false 不以指定元素结束返回true
// boolean equals(Object anObject) 将此字符串与指定的对象比较。
System.out.println( "apple".equals("apple") );//true 比较了字符串存储的值本身,因String类重写了equals()
System.out.println( "apple".equals("apple2") );//false 比较了字符串存储的值本身,因String类重写了equals()
// int hashCode() 返回此字符串的哈希码。
System.out.println( "apple".hashCode() );//获取对象在内存中的哈希码值。哈希值可以当做内存的一个编号,通过一定的算法变为内存地址
// byte[] getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
System.out.println( "abc".getBytes() ); // 返回字符数组
System.out.println( Arrays.toString( "abc".getBytes() ) ); // [97,98,99],因byte[]只存整数
// int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。
System.out.println( "apple".indexOf("p"));//1
// int lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引。
System.out.println( "apple".lastIndexOf("p")); // 2
// boolean isEmpty() 当且仅当 length() 为 0 时返回 true。
System.out.println( "apple".isEmpty() ); // false
System.out.println( "".isEmpty() ); // true
// int length() 返回此字符串的长度。
System.out.println( "apple".length() ); // 5
// String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。
String str = "a|p|p|l|e";
// "|" 属于特殊字符,需要转义
String[] strs = str.split("\\|");//按照指定的方式切割字符串
System.out.println( Arrays.toString(strs) );//[a,p,p,l,e]
// boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。
System.out.println( "apple".startsWith("app") );// true
// String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
System.out.println( "apple".substring(1) ); //pple 从指定下标开始向后截取字符串[1,n]
// String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
System.out.println( "apple".substring(0,3) ); //app 从第一个指定下标开始到第二个指定下标结束(**不包含第二个下标对应的字符**)截取字符串[0,3]
// char[] toCharArray() 将此字符串转换为一个新的字符数组。
System.out.println( "apple".toCharArray() ); // ['a','p','p','l','e']
// String toLowerCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
System.out.println( "APPLE".toLowerCase() ); // apple
// String toUpperCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
System.out.println( "apple".toLowerCase() ); // APPLE
// trim() 返回字符串的副本,忽略前导空白和尾部空白。
System.out.println( " apple ".trim() ); // apple 前后无空格
// static String valueOf(int i) 返回 int 参数的字符串表示形式。
System.out.println( String.valueOf(100) +1 ); // 1001 进行了字符串拼接