1.String
1.1是什么
/**
* String是java.lang.String,使用不需要导包
* String是字符串类,底层是char数组,所以String的特性几乎和数组一致
* 1字符串一旦创建,该字符串对象不能更改
* 2为了提高字符串的访问效率,Java虚拟机使用了一种缓存技术,可以对字符串操作更加简单方便,更加高效(字符串常量池)
* 3字符串会被保存在静态区中的常量池中,可复用性增强
* 当我们用到一个重复的字符串的时候,会去常量池中进行检索,如果有该字符串,则直接指向,如果没有就创建
*
* @author 学到头秃的张张张
*@Date 2021年10月18日下午8:12:43
*/
1.2基本使用
package day_15text;
public class Text01 {
public static void main(String[] args) {
//创建一个字符串对象,直接指向常量池
String string ="ab";
//string是main方法中的局部变量,=赋值只是更改了这个变量的值,并没有更改ab这个字符串对象
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));
}
}
1.3构造方法
package day_15text;
/**
* 构造方法 : 创建String对象的几种方式
* 1直接构造
* 2普通构造
* 3字节数组
* 4字符数组
* @author 学到头秃的张张张
*@Date 2021年10月18日下午8:35:25
*/
public class Text02 {
public static void main(String[] args) {
//1
String s1="xxxx";
//2
String s2=new String("xxxx");
//3字节数组
byte [] bytes = {97,98,99};
String s3 =new String(bytes);
//abc
System.out.println(s3);
//4 字节数组 截取部分
//下标0开始,第二个参数 起始下标(包含),第三个参数 是个数
String s4 =new String(bytes,0,3);
//abc
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.4常用方法
package day_15text;
/**
* 常用方法
* 1 什么方法, 是成员还是静态
* 2 方法名,参数,返回值是什么
* 3 方法功能
*
* @author 学到头秃的张张张
*@Date 2021年10月18日下午8:58:07
*/
public class Text03 {
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);
}
}
}
1.5注意
package day_15text;
/**
* 使用String不推荐进行频繁的字符串拼接操作
* 因为字符串一旦创建不可改变,只要拼接,就会创建新的字符串对象
* 浪费空间,效率还低,就等于是使用定长数组频繁做添加操作一样
*
* @author 学到头秃的张张张
*@Date 2021年10月18日下午8:55:00
*/
public class Text04 {
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,
}
}
package day_15text;
public class Text05 {
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);
}
}
2.StringBuffer和StringBuilder
2.1是什么
/**
* 都在java.lang下, 使用不需要导包
*
* 1 StringBuffer和StringBuilder是什么
* 是一个可变的字符串缓冲区,底层也是数组,只不过该数组会进行自动扩容适合做字符串拼接操作
* 2 原理 :
* 预先在内存中创建一个空间,用来保存字符(字符数组)
* 如果预留空间不够了,会进行自动扩容,用来容纳更多的字符
* 默认长度为16,扩大容量为(原长度+1)*2 : 16 -> 34 -> 70
* 3 StringBuffer和StringBuilder的区别
* StringBuffer : 线程安全,在多线程环境下使用,没有问题
* StringBuilder : 非线程安全,在多线程环境下使用,可能出现问题
*
* @author 学到头秃的张张张
*@Date 2021年10月18日下午9:30:57
*/
2.2使用方式
package day_15text;
public class Text06 {
public static void main(String[] args) {
// 创建对象
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);
}
}