一. string类的概念
在java中 string类是用于表示和操作字符串的类,字符串是java中常用的数据类型,用于存储文本信息,并且string类中提供了许多方法用来执各种字符串操作,那么我们随着下文来学习一下常见的方法。
1.1 string类的特性
1. 不可变性:
string 对象是不可变的,一旦创建了 string 对象,其里面的内容就不能被修改,对 string的任何操作实际上都是创建了一个新的 string 对象。
注意:不能修改指的是:string对象的内容不能修改,而指向string对象的引用是可以修改的
2. 常量池的概念
字符串常量池是java中用于存储字符串字面量的内存区域,为了节省内存,java使用字符常量池来存储字符串字面量,当创建相同的字符串字面量时,它们会引用常量池中的同一个对象,从而降低程序内存的开销。
二. 字符串的构造方式
1. 使用字符串字面量构造
public class test { public static void main(string[] args) { string s1 = "hello world"; //直接使用字符串字面量进行构造 system.out.println(s1); } }
2. 通过new关键字创建一个string对象
public class test { public static void main(string[] args) { string s2 = new string("hello world");//通过new关键字创建对象 system.out.println(s2); } }
3.使用字符数组进行构造
public class test { public static void main(string[] args) { char[] str = {'h','e','l','l','o','w','o','r','l','d'}; string s3 = new string(str); //使用字符数组进行构造 system.out.println(s3); } }
以上就是最常见的最常见的字符串构造方式。
三. 常用方法
3.1 字符串查找
(1)char charat( int index)
public class test { public static void main(string[] args) { string str="hello world"; char ch=str.charat(1); system.out.println(ch);//e } }
如果输入一个不合法的下标位置,就会发生报错:
(2)int indexof( int ch)
public class test { public static void main(string[] args) { string str="hello world"; int n=str.indexof('l'); system.out.println(n); //2 } }
(3)int indexof(int ch, int fromindex)
public class test { public static void main(string[] args) { string str="hello world"; int n=str.indexof('o',6); //从下标为6的位置(w)开始往后查找o的位置,7 system.out.println(n); } }
(4)int indexof(string str)
public class test { public static void main(string[] args) { string str="hello worldwor"; int n=str.indexof("wor"); // 返回wor第一次出现的位置,没有返回-1,所以返回值为6 system.out.println(n); } }
(5)int indexof(string str, int fromindex)
public class test { public static void main(string[] args) { string str="hello worldwor"; int n=str.indexof("wor",8); //从下标为8的位置开始找wor第一次出现的位置,没有返回-1,所以这里返回值为11 system.out.println(n); } }
(6)int lastindexof(int ch)
public class test { public static void main(string[] args) { string str="hello worldwor"; int n=str.lastindexof('w'); //从后往前找,返回w第一次出现的位置,没有返回-1,所以返回值为11 system.out.println(n); } }
(7)int lastindexof(int ch, int fromindex)
public class test { public static void main(string[] args) { string str="hello worldwor"; int n=str.lastindexof('w',10); //从下标10的位置开始找,从后往前找w第一次出现的位置,没有返回-1,所以返回值为6 system.out.println(n); } }
(8)int lastindexof(string str)
public class test { public static void main(string[] args) { string str="hello worldwor"; int n=str.lastindexof("wor"); //从后往前找,返回wor第一次出现的位置,没有返回-1,返回值为11 system.out.println(n); } }
(9)int lastindexof(string str, int fromindex)
public class test { public static void main(string[] args) { string str="hello worldwor"; int n=str.lastindexof("wor",10); //从下标为10的位置开始找,从后往前找wor第一次出现的位置,没有则返回-1,这里返回值为6 system.out.println(n); } }
3.2 字符串转换
(1) 数值和字符串转化
数值转换成字符串,用string.valueof( )进行转换:
public class test { public static void main(string[] args) { // 数字转字符串 string s1 = string.valueof(123456); string s2 = string.valueof(13.14521); system.out.println(s1); system.out.println(s2); } }
数字字符串转换成数字,用 interger.valueof( )进行转换:
public class test { public static void main(string[] args) { // 字符串转数字 int n= integer.valueof("1234"); double m= double.valueof("12.13"); system.out.println(n); system.out.println(m); } }
并且我们也可以传数字字符串,所需要转换的进制数:
public class test { public static void main(string[] args) { // 字符串转数字 int n=integer.valueof("1111",2); //后面的2表示,前面的数字字符串中的数字是2进制的形式,我们转换成10进制形式 //就是15 system.out.println(n); } }
(2)字母的大小写转换
字母大小写转换我们使用string.touppercase----小写转大写 和 string.tolowercase----大写转小写
public class test { public static void main(string[] args) { string s1 = "hello"; string s2 = "hello"; // 小写转大写 system.out.println(s1.touppercase());//hello // 大写转小写 system.out.println(s2.tolowercase());//hello } }
(3)字符串转数组
我们还可以将一个字符串转换成一个数组,使用string.tochararray( )方法进行转换:
public class test { public static void main(string[] args) { string s = "hello"; // 字符串转数组 char[] ch = s.tochararray(); for (int i = 0; i < ch.length; i++) { system.out.print(ch[i]); } } }
(4)格式化字符串
我们可以使用string.format( )方法进行字符串的格式化:
public class test { public static void main(string[] args) { string s = string.format("%d-%d-%d", 2024, 10,30); system.out.println(s); //打印2024-10-30 } }
3.3 字符串比较
当我们比较基本数据类型的对象的时候,使用 == 可以进行比较,但是对于我们的string类型(引用类型)来说,使用 == 进行比较,比较的是引用中的地址:
从上图就可以发现 str1和str2中存放的的字符串是一样的,但是运行结果显示并不相等,那么当我们需要进行字符串比较的时候该怎么办呢?
3.3.1 equals( )方法
equals( )方法的比较方式:按照字典序比较--->比较的是字符大小的顺序
3.3.2 compare to( )方法
compare to( )方法的比较方法:按照字典序进行比较 ----> compare to( )方法与equals( )方法的区别:equals( )方法的返回值是 boolean 类型的值,而 compare to( )方法返回的是 int 类型的值。
1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值。
2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值。
3.3.3 compare toignorecase( )方法
该方法与compare to( )方法没有很大差别,但是compare toignorecase( )方法在进行比较的时候能够忽略字符的大小写:
3.4 字符串替换
3.4.1 replace( )方法
replace( )方法能够将一个字符串中指定的字符替换成一个新指定的字符:
3.4.2 replacefrist( )方法
replacefrist( )方法能够将字符串中指定的第一个字符串替换成新指定的字符串:
3.4.3 replaceall( )方法
replaceall( )方法 与 replacefrist( )方法不同的是: replaceall( )方法能将字符串中所有指定的字符串全部替换成一个新指定的字符串:
3.5 字符串拆分
3.5.1 string[] split(string regex)
string[] split( )方法能够将一个字符串按照指定的字符为分割线,分割成若干个子字符串:
3.5.2 string[] split(string regex, int limit)
string[] split(string regex, int limit)方法能够将字符串按照指定的格式,分割成 limit 个子字符串:
3.6 字符串截取
3.6.1 string substring(int beginindex)
将字符串从下标为 beginindex 的位置开始截取到字符串结尾:
3.6.2 string substring(int beginindex, int endindex)
将字符串从下标为 beginindex 的位置开始截取到 endindex 位置的前一个字符为止:
四.stringbuilder和stringbuffer
在开头的string类的特征中,我们就说明了string类的不可变性,对 string 对象的任何修改操作都会创建一个新的对象,在程序运行的过程中,效率是很低的,那么这个时候如果要对字符串进行修改尽量使用stringbuffer或者stringbuilder。
由于 string 的不可更改特性,为了方便字符串的修改, java 中又提供 stringbuilder 和 stringbuffer 类。这两个类大部分功能是相同的
在这里介绍一下stringbuilder一些常用的方法。
4.1 append(string str)
append(string str)方法可以在字符串的末尾追加新的内容:
注意:追加的不只是字符串,还可以追加:boolean、char、char[]、double、float、int、long、object、string、stringbuff类型的变量。
4.2 setcharat(int index, char ch)
setchatat(int index, char ch)方法能够将 index位置的字符替换成ch:
4.3 reverse( )
将字符串进行反转:
4.4 capacity( )
capacity( ) 方法可以获取底层保存字符串空间总的大小:
4.5 ensurecapacity(int mininmumcapacity)
将该字符串的内存空间扩展到指定的空间大小:
4.6 insert(int offset, string str)
在指定的下标位置插入一个字符串:
4.7 deletecharat(int index)
删除指定下标的字符:
补充:delete(int start, int end)能够删除[start, end)区间内的字符:
4.8 replace(int start, int end, string str)
将[start, end)位置的字符替换为str:
注意:string和stringbuilder类不能直接转换。如果要想互相转换,可以采用如下原则
1. string变为stringbuilder: 利用stringbuilder的构造方法或append()方法
2. stringbuilder变为string: 调用tostring()方法
结语
到此这篇关于java中string类的一些常见方法总结的文章就介绍到这了,更多相关java string类常见方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论