day15_常用API
String
String是 java.lang.String , 使用不需要导包
- String是字符串类,是引用类型,底层是char数组,所以String的特性几乎和数组一致
- 1 字符串一旦创建,该字符串对象不能更改
- 2 为了提高字符串的访问效率,java虚拟机使用了一种缓存技术,可以对字符串操作更加简单方便,更加高效(字符串常量池)
- 3 字符串会被保存在静态区中的常量池中,可以复用性增强
- 当我们用到一个重复的字符串的时候,会去常量池中进行检索,如果有该字符串,则直接指向,如果没有就创建
public class String_01 {
public static void main(String[] args) {
// 创建一个字符串对象,直接指向常量池
String string = "ab";
// string 是main方法中的局部变量, = 赋值只是更改了这个变量的值,并没有更改ab这个字符串对象
//而是开辟新的空间保存"cd"并把值赋值给string
string = "cd";
// 只会创建一个对象 , 就是abc 保存在常量池中
String s1 = "abc";
String s2 = "abc";
// true
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
// 创建了3个对象,堆内存两个,常量池1个
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));
}
}
构造方法 : 创建String对象的几种方式
- 1 直接创建
- 2 普通构造
- 3 字节数组
- 4 字符数组
public class String_02 {
public static void main(String[] args) {
// 1
String s1 = "xxx";
// 2
String s2 = new String("xxx");
// 3 字节数组
byte[] bytes = {97,98,99,100};
String s3 = new String(bytes);
// abcd
System.out.println(s3);
// 4 字节数组 截取一部分
// 下标0开始, 第二个参数 起始下标(包含) , 第三个参数 是个数
String s4 = new String(bytes,1,3);
// bc
System.out.println(s4);
// 5 字符数组
char[] chars = {'q','w','e','r','d','f'};
String s5 = new String(chars);
System.out.println(s5);
// 6 字符数组 截取一部分
String s6 = new String(chars,2,3) ;
System.out.println(s6);
}
}
- 常用方法
- 1 什么方法, 是成员还是静态
- 2 方法名,参数,返回值是什么
- 3 方法功能
public class String_03 {
public static void main(String[] args) {
// 1 char charAt(int index ) : 获取字符串中指定下标的字符
String s1 = "qwer";
char c = s1.charAt(2);
System.out.println(c);
// 2 boolean startsWith(String prefix) : 判断该字符串是否以指定字符串开头
// endsWith : 是否以指定字符串结尾
System.out.println("Hello.java".endsWith(".java"));
// false 注意空格
System.out.println("Hello.java ".endsWith(".java"));
System.out.println("Hello.java".endsWith("a"));
// 3 boolean equals(Object obj) : 判断字符串是否相等
// boolean equalsIgnoreCase(String str) : 不区分大小写判断相等
System.out.println("aAfXCs".equals("aaFxcs"));
System.out.println("aAfXCs".equalsIgnoreCase("aaFxcs"));
// 4 byte[] getBytes() : 把字符串转换为字节数组
byte[] bytes = "abcd".getBytes();
for (byte b : bytes) {
System.out.println(b);
}
// 5 char[] toCharArray() : 把字符串转换为字符数组
char[] chars = "abcd".toCharArray();
for (char d : chars) {
System.out.println(d);
}
// 6 int indexOf(String str) : 获取指定字符串在该字符串中第一次出现的索引,找不到就返回-1
// int lastIndexOf(String str) : 同上,最后一次出现的索引,
int index = "abfacdefabc".indexOf("fa");
System.out.println(index);
index = "abfacdefabc".lastIndexOf("fa");
System.out.println(index);
// 7 int length() : 返回字符串长度
System.out.println("xzjhcaskd".length());
// 8 String[] split(String regex) : 分割字符串,支持正则表达式
String[] strs = "2008,08,09".split(",");
for (String string : strs) {
System.out.println(string);
}
System.out.println("=============");
// 9 String replaceAll(String regex,String replacement) : 替换字符串中的内容为指定文字,支持正则表达式
// replace : 同上,不支持正则表达式
String string = "ahsdhqwajsbasda";
// 替换为空字符串 就等于把a删掉
String newString = string.replace("a", "");
System.out.println(newString);
// 10 String substring(int beginIndex , int endIndex) : 截取字符串,包含开始 "不包含" 结束
// substring(int beginIndex) : 从指定下标开始(包含),到末尾
String s2 = "abcdef";
// cdef
System.out.println(s2.substring(2));
// cd
System.out.println(s2.substring(2,4));
// 11 String toUpperCase() : 转换为大写
// 12 String toLowerCase() : 转换为小写
// 13 String trim() : 去除两边空格
System.out.println(" a b ");
System.out.println(" a b ".trim());
// 多个英文名字,以逗号隔开
String name = "job,rols,xiaoming,Xiaohong";
// 需求 : 获取每一个姓名,并把该姓名以首字母大写进行存储
// 1 以逗号分割,得到封装每个名字的数组
String[] names = name.split(",");
// 2 遍历数组,获取每个姓名,截取字符串,转换为大写,再拼接起来
for (int i = 0; i < names.length; i++) {
String oldName = names[i];
// System.out.println(oldName.substring(0, 1).toUpperCase()+oldName.substring(1));
names[i] = oldName.substring(0, 1).toUpperCase()+oldName.substring(1);
}
for (String string2 : names) {
System.out.println(string2);
}
}
}
- 使用String不推荐进行频繁的字符串拼接操作
- 因为字符串一旦创建不可改变,只要拼接,就会创建新的字符串对象
- 浪费空间,效率还低,就等于是使用定长数组频繁做添加操作一样
public class String_04 {
public static void main(String[] args) {
String[] strs = { "a", "b", "c" };
String str = "";
for (int i = 0; i < strs.length; i++) {
str += strs[i] + ",";
}
System.out.println(str);
// a , b , c , "" , a, , a,b, , a,b,c,
}
}
public class String_05 {
public static void main(String[] args) {
String s1 = "ab";
String a = "a";
String b = "b";
String s2 = a+b;
// false
System.out.println(s1 == s2);
String s3 = "a"+"b";
// true
System.out.println(s3 == s1);
String s4 = new String("ab");
// false
System.out.println(s1 == s4);
}
}
StringBuilder 和 StringBuffer区别
- 都在java.lang下, 使用不需要导包
- 1 StringBuffer和StringBuilder是什么
- 是一个可变的字符串缓冲区,底层也是数组,只不过该数组会进行自动扩容
适合做字符串拼接操作
- 2 原理 :
预先在内存中创建一个空间,用来保存字符(字符数组)
如果预留空间不够了,会进行自动扩容,用来容纳更多的字符
默认长度为16,扩大容量为(原长度+1)*2 : 16 -> 34 -> 70
- 3 StringBuffer和StringBuilder的区别
StringBuffer : 线程安全,在多线程环境下使用,没有问题
StringBuilder : 非线程安全,在多线程环境下使用,可能出现问题
public class StringBuilder_StringBuffer {
public static void main(String[] args) throws Exception {
// 创建对象
StringBuilder sb = new StringBuilder();
// 添加(尾部添加)
sb.append("a");
sb.append("b");
// 插入到指定位置
sb.insert(1, "c");
// 已添加元素个数
System.out.println(sb.length());
// 返回当前容量,默认是16
System.out.println(sb.capacity());
// 反转
sb.reverse();
// 调用 toString 方法 可以转换为字符串
String str = sb.toString();
System.out.println(str);
}
}