Java中的String类

 String类的基本使用

声明

 public final class String implements java.io.Serializable, Comparable<String>, CharSequence

Serializable:可序列化的接口

Comparable:实现此接口的类,其对象都可以比较大小

String内部声明的属性

//jdk8中:

         private final char value;

         //存储字符串数据的容器

//jdk9之后:

         private final byte[] value;

         //对jdk8进行优化,可以节省内存空间,如果存储的字符大小超过byte[],会默认转化为char

字符串常量的存储位置

1.字符串常量都存在串常量池中

2. StringTable中不可存放两个相同的常量

String s1 = "Hello";
String s2 = "Hello";
//此时二者的地址一样

3.StringTable的存放位置:

        jdk7之前:方法区

        jdk7之后:堆空间

String s1 = "Hello";
String s2 = "Hello";
String s2 = "Hi";
//内存中的存储如下图

所以

1.对字符串变量重新赋值时,指定1个字符串常量的位置进行赋值,不能在原来的位置上进行修改

2.对现有的字符串进行拼接操作时,要重新开辟空间

 String实例化的两种方式

//方式1
String s1 = "Hello";

//方式2
String s2 = new String("Hello");

//内存中的存储如下

String中的连接操作:+

使用的方法

intern():返回字符串常量池中字面量的地址

concat():不管参数是什么,都返回一个new的对象

1.常量 + 常量:结果仍然存储在字符串常量池中(字面量或final修饰的变量)

2.常亮 + 变量 or 变量 + 变量:都会通过new的方式创建一个新的字符串,返回堆空间中此字符串的地址

String中的常用构造器

//1.public String():初始化新创建的 String对象,以使其表示空字符序列。
//2.String(String original):初始化一个新创建的 "String"对象,使其表示一个与参数相同的字符序 
    列;换句话说,新创建的字符串是该参数字符串的副本。
//3.public String(char[] value) :通过当前参数中的字符数组来构造新的String。
//4.public String(char[] value,int offset, int count) :通过字符数组的一部分来构造新的String
//5.public String(byte[] bytes) :通过使用平台的""默认字符集""解码当前参数中的字节数组来构造新的String
//6.public String(byte[] bytes,String charsetName) :通过使用指定的字符集解码当前参数中的字节数组来构造新的String




//举列
String str2 = new String(new chae[]{'a','b','c'});

 String中的常用方法

(1)boolean isEmpty():字符串是否为空
(2)int length():返回字符串的长度
(3)String concat(xx):拼接
(4)boolean equals(Object obj):比较字符串是否相等,区分大小写
(5)boolean equalsIgnoreCase(Object obj):比较字符串是否相等,不区分大小写
(6)int compareTo(String other):比较字符串大小,区分大小写,按照Unicode编码值比较大小
(7)int compareToIgnoreCase(String other):比较字符串大小,不区分大小写
(8)String toLowerCase():将字符串中大写字母转为小写
(9)String toUpperCase():将字符串中小写字母转为大写
(10)String trim():去掉字符串前后空白符
(11)public String intern():结果在常量池中共享
(11)boolean contains(xx):是否包含xx
(12)int indexOf(xx):从前往后找当前字符串中xx,即如果有返回第一次出现的下标,要是没有返回-1
(13)int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
(14)int lastIndexOf(xx):从后往前找当前字符串中xx,即如果有返回最后一次出现的下标,要是没有返回-1
(15)int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
(16)String substring(int beginIndex) :返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。 
(17)String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。
(18)char charAt(index):返回[index]位置的字符
(19)char[] toCharArray(): 将此字符串转换为一个新的字符数组返回
(20)static String valueOf(char[] data)  :返回指定数组中表示该字符序列的 String
(21)static String valueOf(char[] data, int offset, int count) : 返回指定数组中表示该字符序列的 String
(22)static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的 String
(23)static String copyValueOf(char[] data, int offset, int count):返回指定数组中表示该字符序列的 String

(24)boolean startsWith(xx):测试此字符串是否以指定的前缀开始 
(25)boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
(26)boolean endsWith(xx):测试此字符串是否以指定的后缀结束 
(27)String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 不支持正则。
(28)String replace(CharSequence target, CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。 
(29)String replaceAll(String regex, String replacement):使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 
(30)String replaceFirst(String regex, String replacement):使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 

String与char[]之间的转换

//String与char[]之间的转换
//String ---> char[]
String str = "Hello";
char[] arr = str.toCharArray();

//char[] ---> String
String str1 = new String(arr);


String与byte[]之间的转换

//使用中文时,如果String直接转化为char[]则会产生乱码,所以我们要先指定字符集
//gdk字符集:1个汉字占了2个字节,1个字母占1个字节
//utf-8字符集:1个汉字占了3个字节,1个字母占1个字节

//String ---> byte[]
byte[] arr = str.getBytes(StandardCharsets.UTF-8);

//byte[] ---> String
//该转换不会乱码
String str = new String(arr);

编码与解码

编码: String ---> 字节字节数组

解码: 字节字节数组 ---> String

        

要求:解码时的字符集与编码时的字符集必须一致,不然就会乱码

String,StringBuffer,StringBuilder

String:不可变的字符序列

StringBuffer:可变的字符序列, jdk1.0声明,线程安全,效率低

StringBuilder:可变的字符序列, jdk 5.0声明,线程不安全,效率高

相同点:底层结构相同,在jdk8之前,底层都使用char[],在jdk8之后,底层使用byte[]

StringBuilder的使用

StringBuffer的内部属性有

char[] value;
//存储字符序列

int count;
//实际存储的字符个数
StringBuilder sBuilder = new StringBuilder();
//默认char[16],如果使用下标存储的字符串超出这个个数的话,那就会报错 
StringBuilder sBuilder1 = new StringBuilder("abc");
//char[16 + "abc".length];

String,StringBuffer,StringBuilder的比较

1.对字符串进行增改等操作,使用StringBuffer,StringBuilder替换掉String,因为前两者的效率高

2.如果开发中不涉及线程安全问题,建议使用StringBuilder,因为开发的效率高

3.如果开发中大体确定要操作的字符串的个数,建议使用int capaity参数的构造器,这样可以避免底层扩容操作,让性能提高

3.三者执行的效率分别为StringBuilder > StringBuffer > String

StringBuffer,StringBuilder的常用方法

1.常用API
(1)StringBuffer append(xx):提供了很多的append()方法,用于进行字符串追加的方式拼接
(2)StringBuffer delete(int start, int end):删除[start,end)之间字符
(3)StringBuffer deleteCharAt(int index):删除[index]位置字符
(4)StringBuffer replace(int start, int end, String str):替换[start,end)范围的字符序列为str
(5)void setCharAt(int index, char c):替换[index]位置字符
(6)char charAt(int index):查找指定index位置上的字符
(7)StringBuffer insert(int index, xx):在[index]位置插入xx
(8)int length():返回存储的字符数据的长度
(9)StringBuffer reverse():反转
2.其它API
(1)int indexOf(String str):在当前字符序列中查询str的第一次出现下标
(2)int indexOf(String str, int fromIndex):在当前字符序列[fromIndex,最后]中查询str的第一次出现下标
(3)int lastIndexOf(String str):在当前字符序列中查询str的最后一次出现下标
(4)int lastIndexOf(String str, int fromIndex):在当前字符序列[fromIndex,最后]中查询str的最后一次出现下标
(5)String substring(int start):截取当前字符序列[start,最后]
(6)String substring(int start, int end):截取当前字符序列[start,end)
(7)String toString():返回此序列中数据的字符串表示形式
(8)void setLength(int newLength) :设置当前字符序列长度为newLength

//总结为增,删,改,查,插,长度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值