当前位置:网站首页>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 去掉字符串中的作呕空格,保留中间空格
边栏推荐
- How to delete or replace the loading style of easyplayer streaming media player?
- deepsort源码解读(四)
- Interpretation of deepsort source code (II)
- 客户案例 | 聚焦流程体验,助银行企业APP迭代
- Two ways of multi GPU training of pytorch
- DNA research experiment application | cyclodextrin modified nucleic acid cd-rna/dna | cyclodextrin nucleic acid probe / quantum dot nucleic acid probe
- newest! SASAC releases new measures for digital transformation of state-owned enterprises
- 基于SSM图书借阅管理系统
- DNA modified near infrared two region GaAs quantum dots | GaAs DNA QDs | DNA modified GaAs quantum dots
- Music website management system based on SSM
猜你喜欢

Web configuration software for industrial control is more efficient than configuration software

基于SSM图书借阅管理系统

基于SSM学生学籍管理系统

DNA修饰贵金属纳米颗粒|脱氧核糖核酸DNA修饰纳米金(科研级)

DNA科研实验应用|环糊精修饰核酸CD-RNA/DNA|环糊精核酸探针/量子点核酸探针

PNA modified polypeptide arms PNA PNA DNA suc aapf PNA suc - (ALA) 3 PNA

CdS quantum dots modified DNA | CDs DNA QDs | near infrared CdS quantum dots coupled DNA specification information

客户案例 | 聚焦流程体验,助银行企业APP迭代

Book borrowing management system based on SSM

Dajiang livox customized format custommsg format conversion pointcloud2
随机推荐
Campus news release management system based on SSM
Digital image processing - Chapter 6 color image processing
DNA modified noble metal nanoparticles | DNA modified gold nanoparticles (scientific research level)
[latex format] there are subtitles side by side on the left and right of double columns and double pictures, and subtitles are side by side up and down
Why can cross entropy loss be used to characterize loss
What is the reason why the channel list is empty on the intelligent security video platform easycvr?
基于SSM学生学籍管理系统
deepsort源码解读(三)
Interpretation of deepsort source code (II)
VIVO应用市场APP上架总结
Neural network parameter initialization
Shell编程的规范和变量
Introduction to the official functions of easyrecovery14 data recovery software
Deepsort工作原理分析
What is the reason why dragging the timeline is invalid when playing device videos on the easycvr platform?
CASS11.0.0.4 for AutoCAD2010-2023免狗使用方法
Customer cases | focus on process experience to help bank enterprise app iteration
DNA修饰贵金属纳米颗粒|脱氧核糖核酸DNA修饰纳米金(科研级)
CdS quantum dots modified DNA | CDs DNA QDs | near infrared CdS quantum dots coupled DNA specification information
Boostrap