Home >Java >javaTutorial >How to use Java commonly used string-related classes
String, StringBuilder, and StringBuffer classes are three string related classes.
The String class represents an immutable character sequence, and the StringBuilder class and StringBuffer class represent mutable character sequences.
The detailed usage of these three categories is often used in written examinations and interviews as well as actual development. We must master it.
Common methods of String:
1.isEmpty() returns true if the string is empty, otherwise returns false
2.length() Calculate the string length
3.isBlank() This method returns true if the given string is empty or contains only blank code points, otherwise it returns false
4.startsWith() Whether it starts with the string in brackets
5.endsWith() Whether it ends with the string in brackets
6.toLowerCase() generates a new string , all the English characters in the string are changed to lowercase
7.toUpperCase() generates a new string, all the English characters in the string are changed to uppercase
8.charAt() returns the specified index position char value. The index range is 0~length()-1
9.substring(int startIndex) The substring starts from the index
10.substring(int startIndex, int endIndex) returns a string, This string is a substring of this string.
The substring starts at the specified beginIndex and extends to the character index endIndex- 1
11.public int indexOf (int ch) Returns the first occurrence of the specified character in the string Index
12.indexOf(String str, int fromIndex) Returns the index in the string of the first occurrence of the specified substring, starting from the specified index
13.lastIndexOf from backward Query the index position of the specified string encountered for the first time. Note that the index is still counting from front to back
14.split() method: split the string, the parameter regex is called the separator, you can use regular Expression to represent
15.replace() is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression. Case sensitive
16.replaceAll(String regex,String replacement) Replaces every substring of this string that matches the given regular expression with the given replacement. regex - the regular expression to match this string, replacement - to replace each matching word
17.trim() removes the spaces on both sides of the string
18.toCharArray() will Convert the string to a character array
19.concat() method, append the substring at the end of the string
20.contains() if and only if this string contains the specified char value sequence, returns true
21.compareTo() returns the difference in ASCII code of the two strings before and after the comparison. If the first letters of the two strings are different, this method returns the ASCII code of the first letter. Difference value, if the first characters are the same, compare the next characters until there is a difference, and return the ASCII code difference value of the different characters. If the two strings are not the same length and the characters that can be compared are exactly the same, the length difference of the two strings is returned.
Example 1:
package li.normalclass.stringclass; import java.util.Arrays; public class TestString { public static void main(String[] args) { // 1.如何创建String对象 String str = "北京天安门abc"; // 2.如何使用String对象 // 2.1最简单的方法 System.out.println(str.length() );//8 注意是字符的个数,不是字节的个数 //如果字符串为空返回 true,否则返回 false System.out.println(str.isEmpty());//false //jdk11新增的方法,如果给定的字符串为空或仅包含空格代码点,则此方法返回 true ,否则返回 false System.out.println(str.isBlank());//false //是否已括号内的字符串为开始 System.out.println(str.startsWith("北京天"));//true // 是否已括号内的字符串为结束 System.out.println(str.endsWith("c"));//true //生成一个新的字符串,字符串的英文字符全部变小写 System.out.println(str.toLowerCase()); //北京天安门abc //生成一个新的字符串,字符串的英文字符全部变大写 System.out.println(str.toUpperCase());//北京天安门ABC /* 注意:String是不可变字符序列,上面的方法改变的只是新生成的字符串,这里重新输出str,依旧是原来的字符 */ System.out.println(str);//北京天安门abc //2.2根据索引找子串 //charAt()方法返回指定索引位置的char值。索引范围为0~length()-1 char c = str.charAt(3);//注意下标从0开始 System.out.println(c);//安 //str.substring(int startIndex); //子字符串的下标从索引开始 System.out.println(str.substring(2));//天安门abc //substring(int startIndex,int endIndex); /*返回一个字符串,该字符串是此字符串的子字符串。 子串开始于指定beginIndex并延伸到字符索引endIndex- 1 因此,子串的长度为endIndex-beginIndex */ System.out.println(str.substring(5,8));//abc // 2.3根据子串找索引 //public int indexOf(int ch)返回指定字符第一次出现的字符串内的第一个索引 int index = str.indexOf("abc"); System.out.println(index);//5 //indexOf(String str, int fromIndex) //返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。 System.out.println(str.indexOf("门", 2));//4 //从后向前查询第一次遇到的指定字符串的索引位置,注意索引还是从前往后数起 System.out.println(str.lastIndexOf("北"));//0 // 2.4其他方法 /* str.concat(); str.trim(); str.split(); str.replace(); str.replaceAll()等 */ //split(String regex) //split()方法:分割字符串,参数regex称为分割符,可以使用正则表达式来表示 String str2 = "Java,HTML,MySQL,Spring,java,Java"; String arr [] = str2.split("S"); System.out.println(Arrays.toString(arr));//[Java,HTML,My, QL,, pring,java,Java] //replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。区分大小写 System.out.println(str2.replace("Java","javase"));//javase,HTML,MySQL,Spring,java,javase //public String replaceAll(String regex,String replacement) //用给定的替换替换与给定的regular expression匹配的此字符串的每个子字符串。 //regex - 要匹配此字符串的正则表达式, replacement - 要替换每个匹配的字 String str3 = "abc,adc,afffc,rty,acc"; String str4 = str3.replaceAll("a...c","#"); System.out.println(str4);//abc,adc,#,rty,acc //trim()去掉字符串两边的空格 String str5 = " rbg ni men hao "; System.out.println(str5.length());//27 System.out.println(str5.trim());//去掉字符串两端的空格 "rbg ni men hao" System.out.println(str5.trim().length());//21 //toCharArray() char [] chArr = str.toCharArray();//str = "北京天安门abc" System.out.println(chArr); System.out.println(chArr[2]);//天 //concat()方法,在字符串的末尾追加子串 String str6 = "北京市"; str6 = str6.concat("紫禁城").concat("故宫").concat("博物院"); System.out.println(str6);//北京市紫禁城故宫博物院 //contains() 当且仅当此字符串包含指定的char值序列时,返回true System.out.println( str6.contains("博物院"));//true /*compareTo()方法 返回比较的前后两个字符串的ASCII码的差值,如果两个字符串首字母不同,则该方法返回首字母的ASCII码的差值 如果首字符相同,则比较下一个字符,直到有不同的为止,返回该不同的字符的ASCII码差值。 如果两个字符串不一样长,可以参与比较的字符又完全一样,则返回两个字符串的长度差值。 返回为正数表示a1>a2, 返回为负数表示a1<a2, 返回为0表示a1==a2 */ String str1 = "jsdy"; String str2 = "jsdr"; System.out.println(str1.compareTo(str2));//7 } }
Example 2: equals and double equals sign==
package li.normalclass.stringclass; public class TestString2 { public static void main(String[] args) { //equals String str1 = new String ("jsdy"); String str2 = new String ("jsdy"); System.out.println(str1==str2);//false System.out.println(str1.equals(str2));//true String str3 = "jsdy"; String str4 = "jsdy"; System.out.println(str3==str4);//ture!!! System.out.println(str3.equals(str4));//true String str5 = new String ("jsdy"); String str6 = "jsdy"; System.out.println(str5==str6);//false System.out.println(str5.equals(str6));//true String str7 = null;//没有指向任何内容 String str8 = new String(""); String str9 = "";//指向一个空字符串 System.out.println(str9.length());//0 //System.out.println(str7.length())-->java.lang.NullPointerException System.out.println(str8==str9);//false System.out.println(str8.equals(str9));//true } }
Analysis:
String str3 = "jsdy"; String str4 = "jsdy"; System.out.println(str3==str4);//ture!!!
Create a character using a literal value string, the JVM will first go to the string pool to find whether the "jsdy" object exists.
If it does not exist, it will create the "jsdy" object in the string pool, and then add "jsdy" to the pool. The reference address of this object is returned to the reference str3 of the "jsdy" object, so that str3 will point to the string object "jsdy" in the pool;
If it exists, no object will be created, and "jsdy" in the pool will be directly added. "The address of this object is returned, assigned to reference str4. Because str3 and str4 both point to the "jsdy" object in the same string pool, the result is true.
String str1 = new String ("jsdy"); String str2 = new String ("jsdy"); System.out.println(str1==str2);//false System.out.println(str1.equals(str2));//true
When using the new keyword to create a string object, the JVM first searches for the string object "jsdy" in the string pool.
If there is, it will not be in the pool again. To create the "jsdy" object, create a "jsdy" string object directly in the heap, and then return the address of the "jsdy" object in the heap to the reference str1. In this way, str1 points to the string object created in the heap. This "jsdy" string object;
If not, first create a "jsdy" string object in the string pool, then create a "jsdy" string object in the heap, and then add the heap The address of the "jsdy" string object in the string is returned and assigned to the str1 reference. In this way, str1 points to the "jsdy" string object created in the heap. str2 points to another "jsdy" string object created in the heap. str1 and str2 are two references pointing to different objects, and the result is of course false.
Others are the same.
Example 3:
//concat()方法,在字符串的末尾追加子串 String str6 = "北京市"; str6 = str6.concat("紫禁城"); str6 = str6.concat("故宫"); str6 = str6.concat("博物院"); System.out.println(str6);//北京市紫禁城故宫博物院
As shown above:
采用字面值的方式创建一个字符串时,JVM首先会去字符串池中查找是否存在"北京"这个对象,如果不存在,则在字符串池中创建"北京"这个对象,然后将池中"北京"这个对象的引用地址返回给"北京"对象的引用str6。使用concat()方法可以追加子字符串,但是String是不可变长序列,所以是实际上是在常量池重新创建了一个对象,并把追加的字符串连同原字符串一同赋值给新的对象,然后将新对象的引用地址返回给str6,这样str6就指向了一个新的地址空间。每次使用concat()方法追加子串都会经历上述过程,str6的指向不断改变,最终会指向最后一次开辟的对象地址。
因此使用concat()追加子串的方法效率无疑是很低的,那么有没有一种办法可以直接在创建的对象里添加子串呢?这就是我们要涉及到的StringBuilder类
String类是一个final类,意味着该类不能有子类
String类底层是一个字符数组value。各种方法的操作其实都是对该数组的操作。
String类的equals()方法其实就是比较底层的字符数组的各个元素是否相同,只要发现一个元素不同,就返回false,如果所有字符都相同就返回true。但是如果两个变量都指向了同一个字符数组,则直接返回true。
String类的concat()方法是创建一个新的字符数组,存放原来字符数组和新加入的字符数组内容,然后以该新数组创建一个新的字符串。
JDK9时String类底层由char数组变为byte数组,节省空间。同时通过一个coder成员变量作为编码格式的标识,使用LATIN1还是UFT-16,这个是在String生成时自动的,如果字符串中都是能用LATIN1就能表示的是0,否则就是UFT-16。
StringBuffer和StringBuilder非常类似,均代表可变的字符序列。
这两个类都是抽象类AbstractStringBuilder的子类,方法几乎一模一样
两个类的主要区别是:
StringBuffer JDK1.0提供的类,线程安全,做线程同步检查,效率较低
StringBuilder JDK1.5提供的类,线程不安全,不做线程同步检查,因此效率较高。建议采用此类
StringBuilder常用函数:
append() 向字符串后追加一个子串
reverse() 倒置
delete() 删除从start(包含)到end(不包含)位置的字符, start 为0~length-1
length() 获取字符的长度
toString() 将StringBuffer转成String
replace() 从start到end之间的字符串替换成新字符串
insert() 在指定的偏移量位置插入值
indexOf() 从头开始查找某个字符串在源字符串中第一次出现的位置并返回
setCharAt() 设置指定索引位置的字符
charAt() 返回指定索引位置上的字符
substring() 从start(包含)位置截取字符串返回一个新的String,它包含此序列当前所包含字符的子序列
例子:
package li.normalclass.stringbuilder; /* StringBuilder用得比较多的基本上就是这三个常见操作: 1.创建对象 StringBuilder builder = new StringBuilder("xxx"); 2.末尾追加字符串 builder.append("yyy"); 3.转换为字符串 String str = builder.toString() System.out.println(str) */ public class TestStringBuilder1 { public static void main(String[] args) { //创建StringBuilder对象 /* 创建Builder对象时,底层的数组大小实际为输入的字符串长度个数+16 */ StringBuilder builder = new StringBuilder("北京"); //length是字符的个数,capacity是底层数组的长度 System.out.println(builder.length()+"\t"+ builder.capacity());//2 (2+16=)18 //操作StringBuilder对象 //操作:字符串末尾增加 builder.append("故宫博物院"); System.out.println(builder.length()+"\t"+ builder.capacity());//7 18 builder.append("墙角下的"); //---->这里扩容了,扩容方法是:当前字符串长度*2+2,在这里既是18*2+2=38 System.out.println(builder.length()+"\t"+ builder.capacity());//11 38 builder.append("一只懒猫在睡觉觉"); System.out.println(builder.length()+"\t"+ builder.capacity());//19 38 //操作:字符串中间位置增加 int i = builder.indexOf("下");//找到字符串的数组下标 builder.insert(i,"一棵银杏树");//在下标前插入新的子串 System.out.println(builder.length()+"\t"+ builder.capacity());//24 38 //操作:字符串修改 int i2 = builder.indexOf("银杏树");//找到字符串的数组下标 builder.replace(i2,i2+3,"芒果树");//要替换的字符串的起始位置,结束位置,要替换的字符串 :北京故宫博物院墙角一棵芒果树树下的一只懒猫在睡觉觉 //操作:字符串删除 builder.deleteCharAt(23);//参数为要删除的那个字符的索引下标 :北京故宫博物院墙角一棵芒果树下的一只懒猫在睡觉 builder.delete(0,7);//start并延伸到字符索引end - 1:墙角一棵芒果树下的一只懒猫在睡觉子串开始于指定 //操作:字符串输出 String str = builder.toString();//将StringBuilder转变为一个字符串 System.out.println(str);//墙角一棵芒果树下的一只懒猫在睡觉 System.out.println(builder.toString());//墙角一棵芒果树下的一只懒猫在睡觉 System.out.println(builder);//墙角一棵芒果树下的一只懒猫在睡觉 System.out.println(builder.reverse());//觉睡在猫懒只一的下树果芒棵一角墙 System.out.println(builder);//觉睡在猫懒只一的下树果芒棵一角墙--->没有创建新的字符串对象 } }
注意实际开发过程中StringBuilder的使用场合:字符串的拼接(SQL语句)
StringBuilder用得比较多的基本上就是这三个常见操作:
//1.创建对象(String-->StringBuilder) StringBuilder builder = new StringBuilder("xxx"); //2.末尾追加字符串 builder.append("yyy"); //3.转换为字符串(StringBuilder--->String) String str = builder.toString() System.out.println(str);
StringBuilder的底层就是一个长度可以自动增长的字符数组(JDK9变成了字节数组)
StringBuilder类底层和String类一样,也是一个字符数组value,但不是final的。变量count表示的是底层字符数组的元素的真实个数,不是底层字符数组的长度。
默认数组的长度是16。也可以通过构造方法直接指定初始长度。length()方法返回的是字符数组元素的真实个数,capacity()返回的是底层数组的长度。
添加字符串时如果内存大小不够要扩容,扩容的默认策略是增加到原来长度的两倍再加2
快捷键Ctrl+Alt+向左箭头<·····可以实现跳转到刚刚浏览的那个文件的那行代码
例子1:StringBuilder构造函数
StringBuilder builder = new StringBuilder(); //StringBuilder 的无参构造初始容量为:16
例子2:new StringBuilder
//创建Builder对象时,底层的数组大小实际为输入的字符串长度个数+16 StringBuilder builder = new StringBuilder("故宫博物院"); System.out.println(builder.length()+"\t"+ builder.capacity());
例子3:toString
String str = builder.toString(); System.out.println(str);
将builder的字符转换为String字符串
例子4:append()
略。
总结
String:不可变字符序列
StringBuffer:可变字符序列,并且线程安全,但是效率低
StringBuilder:可变字符序列,线程不安全 ,但是效率高(一般用它)
The above is the detailed content of How to use Java commonly used string-related classes. For more information, please follow other related articles on the PHP Chinese website!