Java:字符串的常用方法介绍–收藏它就对了
文章目录
大家好,我是帅气又迷人的thadqy。那么接下来我会继续为大家介绍Java的一些基本知识,大家可以根据我的文章来熟悉或者是回忆巩固一下Java的基础知识。接下来我会为大家介绍一下Java字符串中的常用方法。希望对大家由一定的帮助,也希望大家多多支持。在这里thadqy先谢过大家啦。
在Java SE中,提供了三个字符串类:String、StringBuffer、StringBuilder。其中String类是不可变字符串,StringBuffer与StringBuilder是可变字符串。
String(不可变字符串)
Java中不可变字符串类是String,属于java.lang包,是Java中非常重要的类。
构造方法
-
String():使用空字符串创建并初始化一个新的String对象
-
String s1 = new String();
-
String(String str):使用另一个字符串创建并初始化一个新的字符串对象
-
String s2 = new String("hello world");
-
String(StringBuffer buffer):使用可变字符串创建并初始化一个新的字符串
-
String(byte[] bytes):使用一个字节数组创建并初始化一个新的字符串
-
byte[] bytes = {97, 98, 99}; String s3 = new String(bytes);
-
String(char[] value):使用字符数组创建并初始化一个新的字符串
-
char[] value = {'a', 'b', 'c', 'd'}; String s4 = new String(value);
-
String(char[] value, int offset, int count):通过数组的子数组创建并初始化一个新的字符串,offset是子数组的第一个字符索引,count是子数组的长度
-
char[] value = {'a', 'b', 'c', 'd', 'e', 'f'}; String s5 = new String(value, 1, 4);
字符串池
在Java,对象是使用new关键字创建的,字符串也可以使用new关键字创建。同时,Java中的字符串也可以使用字符串常量来创建。例如:
-
//使用字符串常量创建字符串 String s1 = "hello world";
-
//使用new关键字创建字符串 String s2 = new String("hello world");
Java中使用字符串常量和new关键字创建字符串是拥有一定的差别的。首先我们可以运行下面的代码:
String s1 = "hello";
String s2 = "hello";
Stirng s3 = new String("hello");
Stirng s4 = new String("hello");
System.out.println("s1 == s2:" + (s1 == s2));
//s1 == s2:true
System.out.println("s1 == s3:" + (s1 == s3));
//s1 == s3:false
System.out.println("s3 == s4:" + (s3 == s4));
//s3 == s4:false
==运算符比较的是两个引用是否指向相同的对象,从上面的结果可见,s3,s4指的是不同的对象,s1、s2指的是相同对象。
这是因为Java中的不可变字符串String常量,采用了字符串池(String Pool)管理技术,字符串池是一种字符串驻留技术。采用字符串常量赋值时,如下图所示,会在字符串池中查找“hello”刺符传常量,如果已经存在就把引用赋值给s2,如果不存在就创建“hello”字符串对象,并放到字符串池中。所以s1和s2是指向相同的引用。但是,采用new关键字创建的s3、s4并不适用。每使用一次new关键字,就会创建一个字符串对象,并且是不同的空间地址,所以,使用new关键字创建字符串的s3、s4是指向不同的对象。
字符串拼接
String字符串虽然是不可变的,但是可以进行拼接,会产生一个新的对象。String字符产使用运算符”+“或者concat(String str)方法进行字符串的拼接。其中”+“运算符可以进行任何字符串的拼接,但是concat(String str)只适用于String类型的字符串拼接。
字符串拼接示例如下:
//使用运算符”+“进行拼接
String s1 = "hello";
String s2 = s1 + "world";
System.ou.println(s2);
//helloworld
//使用concat(String str)进行拼接
String s3 = "hello";
String s4 = s3.concat("world");
//helloworld
String字符串的常用方法函数
-
int indexOf(char ch):从前往后搜索字符ch,返回第一次出现的索引
-
String s = "Java is cool"; System.out.println(s.indexOf('a')); //1
-
int indexOf(String str):从前往后搜索字符串str,返回第一次出现的索引
-
String s = "Java is cool"; System.out.println(s.indexOf("a is")); //3
-
int indexof(String str, int FromIndex)从FromIndex开始,从前往后搜索字符产,返回第一次出现的索引
-
int lastIndexOf(char ch):从后往前搜索字符ch,返回第一次出现的索引
-
String s = "Java is cool"; System.out.println(s.lastIndexOf('a')); //3
-
int lastIndexOf(char ch, int FromIndex):从后往前,从FromIndex开始搜索字符ch,返回第一次出现的索引
-
char charAt(int index):返回字符串索引index处的字符
-
String s = "hello world"; System.out.println(s.charAt(3)); //l
-
boolean stratsWith(String str)以字符串str开始
-
String s1 = "startwith" System.out.println(s1.startsWith("start"));//True
-
boolean endsWith(String str)以字符串str结束
-
String s1 = "startwith" System.out.println(s1.endsWith("with"));//True
-
boolean contains(cahr ch)当字符串中包含字符ch时返回true
-
String s1 = "Hello World"; System.out.println(s1.contains("World"));//True
-
concat(String str)在字符串后面加上字符串str
-
String s1 = "Hello World"; String s2 = "!!!"; System.out.println(s1.concat(s2));//Hello World!!!
-
String.valueOf(char)将字符数组改为字符串
-
char[] ch = {'A', 'B'}; System.out.println(String.valueOf(ch)); //AB
-
getbytes()将字符串改为字节数组
-
String s1 = "Hello World"; byte[] ch1 = s1.getBytes(); for(byte b:ch1) { System.out.print(b + " "); } //72 101 108 108 111 32 87 111 114 108 100
-
hashCode()返回此字符串的哈希码
-
String s1 = "Hello World"; System.out.println(s1.hashCode()); //-862545276
-
isEmpty()判断字符串是否为空
-
String s1 = "Hello World"; String s4 = ""; System.out.println(s4.isEmpty());//True System.out.println(s1.isEmpty());//False
-
join()返回一个新字符串
-
List<String> list = new LinkedList<String>(); list.add("java"); list.add("is"); list.add("cool"); String s5 = String.join(" ", list); System.out.println(s5); // java is cool
-
int length()返回字符串长度
-
String s1 = "Hello World"; System.out.println(s1.length()); //11
-
split(String regex)根据给定的regex分割字符串
-
String s6 = "abcd:erfg:hijk"; String[] s = s6.split(":"); for (String a : s) { System.out.print(a + " "); } //abcd erfg hijk
-
subString(int beginIndex, int endIndex)返回从beginIndex开始(包含)到endIndex结束(不包含)字符串的子串
-
String s1 = "Hello World"; System.out.println(s1.substring(3, 6)); //lo
-
toCharArray()将字符串变为字符数组
-
char[] ch11 = s1.toCharArray(); for (char c : ch11) { System.out.print(c + " "); } //H e l l o w o r l d
字符串的比较方法
-
compareTo(String anotherString)按字典顺序比较两个字符串的大小。如果参数串等于此串等于0;参数串小于此串小于0;参数串大于此串大于0
-
String s4 = "abcdef"; String s5 = "abdefc"; String s6 = "abbcde"; String s7 = "abcdef"; String s8 = "Abcdef"; System.out.println(s4.compareTo(s5));//-1 System.out.println(s4.compareTo(s6));//1 System.out.println(s4.compareTo(s7));//0 System.out.println(s4.compareTo(s8));//32
-
compareToIgnoreCase(String str):忽略大小写进行字典顺序的比较
-
System.out.println(s4.compareToIgnoreCase(s8));//0
StringBuilder、StringBuffer(可变字符串)
可变字符串在追加、删除、修改、插入和拼接等操作不会产生新的对象
Java提供了两个可变字符串StringBuffer、StringBuilder,中文翻译为“字符串缓冲区”。
StringBuffer是线程安全的,他的方法是支持线程同步的,但是在单线程环境下会影响效率。StringBuilder是单线程版本的StringBuffer,不过,它不是线程安全的,但是它的执行效率很高。
StringBuffer、StringBuilder具有相同的API,即相同的构造方法和普通方法等内容一样。
构造方法
-
StringBuffer():构造一个没有字符的字符串缓冲区,初始化容量为16
-
StringBuffer s = new StringBuffer();
-
StringBuffer(String str):构造一个初始化为str相同字符的字符串缓冲区
-
StringBuffer s = new StringBuffer("hello world");
-
StringBuffer(int capacity):构造一个指定容量但是没有字符的字符串缓冲区
-
StringBuffer s = new StringBuffer(10);
StringBuffer方法函数
StringBuffer与String具有很多相同的方法函数。
与String相同用法的方法函数
- int length()
- int indexOf(char ch)
- int indexOf(String str)
- lastIndexOf(String str)
- lastIndexOf(char ch)
- subString()
- charAt()
StringBuffer其他方法函数
-
向字符串中添加元素
-
append(object o):追加元素到字符串,元素可以是任何类型
-
StringBuffer sb = new StringBuffer(); String s = "fouashfo"; sb.append(s); sb.append(" ").append("false"); System.out.println(sb); //fouashfo false
-
insert(int index, object o):将元素o添加到字符串索引为index处
-
StringBuffer s = new StringBuffer("I Java"); String s1 = "Love "; s.insert(2, s1); System.out.println(s); //I Love Java
-
-
capacity():返回字符串的容量
-
StringBuffer s = new StringBuffer(); System.out.println(s.capacity); //16
-
delete(int start, int end):删除子序列从start开始(包含),end结束(不包含)的值
-
StringBuffer s = new StringBuffer("Java is very cool"); s.delete(8, 12); System.out.println(s); //ava is cool
-
deleteCharAt(int index):删除字符串指定索引处的值
-
StringBuffer s = new StringBuffer("Java is cool"); s.deleteCharAt(3); System.out.println(s); //Jav is cool
-
replace(int start, int end, String newStr):用指定字符串替换此字符串中总从start开始到end结束(不包含)的字符
-
StringBuffer s = new StringBuffer("Python is cool"); s.replace(0, 6, "Java"); System.out.println(s); //Java is cool
-
reverse():将字符串逆序输出
-
String s1 = "Java is cool"; String[] s11 = s1.split(" "); for (int i = 0; i < s11.length; i++) { StringBuffer s12 = new StringBuffer(s11[i]); System.out.print(s12.reverse() + " "); } //avaJ si looc
-
toString():返回表示此序列中的数据的字符串
-
trimtoSize()将序列的存储空间容量变为与字符串长度一样
-
StringBuffer s = new StringBuffer("the java is very cool"); System.out.println(s.capacity()); s.trimToSize(); System.out.println(s.capacity()); System.out.println(s.length()); //37 //21 //21
结束语
以上便是我对Java的String类型的记忆,希望能够对读者由一定的帮住。