当前位置:网站首页>String类的用法
String类的用法
2022-07-27 05:39:00 【mmmenxj】
1.String是final修饰的,final修饰的类不能有子类
2.创建字符串的三种方式
String s = "hello word";
String s = new String("hello word");
char[] data ={'h','e','l','l','o'};
String s = new String(data); 字面量:直接写出来的值就称为字面量
10 整形字面量
11.2浮点型字面量
“”字符串字面量
3.字符串的内存布局
String是一个引用数据类型,默认是null,并且对象在堆上存储
4.字符串比较相等时,使用equals方法,不能直接使用==
对于基本数据类型,==比较就是两个变量值,
对于引用数据类型,银两变量保存的是堆内存的地址而不是值,不能使用==比较内容。
==比较地址:

equals比较值:

5.字符串的常量值

产生的原因是什么?
JVM会对字符串创建一个字符串的常量池,当使用直接赋值创建字符串时,字面量第一次出现就会创建一个String对象并且扔入常量池中,当字面量第二次出现或者被多次引用时,并不会创建新对象,而是复用已有对象。

这个结果出现的原因是什么呢?
有new 就有新空间,所以在比较地址时一定不相等。
通过其他方式创建字符串时,可以手工入池,使用intern方法

总结:使用String创建对象就会使用直接赋值法,比较内容是否相等调用equals方法
6.字符串的“不可变”性

此处只是s的指向发生了变化,指向了新的字符串对象"world",而对原字符串"hello"没有任何影响
为何String对象的内容无法修改?
String对象内容实际上存储在内部value[]中,value[]是一个private+final修饰的,value[]出了String类就不可见,无法使用。想要访问私有数据域,必修有getter方法,但是String类没有提供getter方法,外部无法使用value数组。final保证了value指向的地址不可变。
要想修改字符串变量的内容,只能通过反射破坏封装。
如果实在要修改字符串,可以使用substring()方法截取字符串。substring创建了一二个新的字符串变量,并不是修改原来的字符串对象。
由于字符串不可变性,弱国需要大量修改字符串内容时,我们使用两个sb类:
StringBuffer:线程安全
StringBuild:线程不安全,效率高
这两个SB类和String没有任何关系,这是两种不同的类型,千万不要混为一谈。
String ->StringBuild:调用SBuild的append或者通过构造方法传入String对象
//String转StringBuild
//通过构造方法将String转StringBuild
StringBuilder sb = new StringBuilder("hello");
//调用append方法将String变为StringBuild
sb.append("world");
sb.append("!!!");
//此时sTRINGbuild内部存储的就是helloword!!!StringBild ->String
//将SB还原为String
String ret = sb.toString();StrngBuild具备一些String不具备的方法:(StringBuild内容可改,String内容不可改)
1.reverse 字符串反转
2.delete(int start,int end)删除指定范围内的内容 java中一般都是左闭右开区间
3.insert(int start,插入的数据)
7.String和char[]的相互转化
String ->char[]
取得String某一个具体的字符 Strng ->char[]
char[] ->String
String(char value[]) 将字符数组的所有内容变为字符串
String(char value[],int offset,int count)将部分字符数组的内容变成字符串
char charAt(int index)取得指定索引的字符
char[] toCharArray() 将字符串变为字符数组返回
8.char:处理文本内容时使用char
byte:一般用在网络传输或者输入输出使用,判断网速,默认单位就是byte
9.其他常用操作
equals:区分大小写的比较
eauqlsIgnoreCase:不区分大小写的比较
compareTo:比较两个字符串的大小关系
10.字符串的替换
String类的所有修改操作的内部都是创建一个新的字符串,字符串的不可变性!!无法修改原字符串。
repalceAll 替换所有字母
public class StringCode {
public static void main(String[] args) {
String s = "hello";
String s1 = s.replaceAll("l","-");
System.out.println(s1);
}replaceFirst 替换首字母
public class StringCode {
public static void main(String[] args) {
String s = "hello";
String s1 = s.replaceFirst("l","-");
System.out.println(s1);
}拆分操作:s.split
public class StringCode {
public static void main(String[] args) {
String s = "hello mxj hello java";
String[] data = s.split(" ");//按照空格拆分
String[] data1 = s.split(" ",2);//按照空格拆分,最多拆成两个字符串
System.out.println(Arrays.toString(data));
System.out.println(Arrays.toString(data1));
}
}特殊字符需要先转义:
String s = "192.168.1.1";
//.属于java中的特殊操作符,访问操作符
//此时需要转义处理
String[] data = s.split("\\.");//需要转义
System.out.println(Arrays.toString(data));其他操作:trim 去掉字符串中的作呕空格,保留中间空格
边栏推荐
- Iotdb C client release 0.13.0.7
- [unity URP] the code obtains the universalrendererdata of the current URP configuration and dynamically adds the rendererfeature
- R2LIVE代码学习记录(3):对雷达特征提取
- How can chrome quickly transfer a group of web pages (tabs) to another device (computer)
- DNA modified near infrared two region GaAs quantum dots | GaAs DNA QDs | DNA modified GaAs quantum dots
- TS learning (VIII): classes in TS
- Watermelon book chapter 3 - linear model learning notes
- 最新!国资委发布国有企业数字化转型新举措
- Digital image processing - Chapter 6 color image processing
- About the new features of ES6
猜你喜欢

Student achievement management system based on SSM

【12】 Understand the circuit: from telegraph to gate circuit, how can we "send messages from thousands of miles"?

DNA(脱氧核糖核酸)供应|碳纳米管载核酸-DNA/RNA材料|DNA/RNA核酸修饰磁性纳米颗粒

Book borrowing management system based on SSM

Derivative, partial derivative and gradient

基于SSM图书借阅管理系统

Event capture and bubbling - what is the difference between them?

Significance of NVIDIA SMI parameters

Talk about multimodality of fire

AI:业余时间打比赛—挣它个小小目标—【阿里安全×ICDM 2022】大规模电商图上的风险商品检测比赛
随机推荐
PNA modified polypeptide arms PNA PNA DNA suc aapf PNA suc - (ALA) 3 PNA
How to make the minimum API bind the array in the query string
ZnS-DNA QDs近红外硫化锌ZnS量子点改性脱氧核糖核酸DNA|DNA修饰ZnS量子点
deepsort源码解读(三)
Basic concepts of program, process, thread, coprocess, single thread and multi thread
聊聊大火的多模态
Deepsort工作原理分析
PNA polypeptide PNA TPP | GLT ala ala Pro Leu PNA | suc ala Pro PNA | suc AAPL PNA | suc AAPM PNA
Sok: the faults in our asrs: an overview of attacks against automatic speech recognition
About the new features of ES6
Digital image processing - Chapter 6 color image processing
Pytorch model
DataScience:数据生成之在原始数据上添加小量噪声(可自定义噪声)进而实现构造新数据(dataframe格式数据存储案例)
Cass11.0.0.4 for autocad2010-2023 dog free usage
Day012 一维数组的应用
Iotdb C client release 0.13.0.7
Error in running code: libboost_ filesystem.so.1.58.0: cannot open shared object file: No such file or directory
DNA coupled PbSe quantum dots | near infrared lead selenide PbSe quantum dots modified DNA | PbSe DNA QDs
脱氧核糖核酸DNA修饰氧化锌|DNA修饰纳米金颗粒|DNA偶联修饰碳纳米材料
齐岳:巯基修饰寡聚DNA|DNA修饰CdTe/CdS核壳量子点|DNA偶联砷化铟InAs量子点InAs-DNA QDs