JAVA study notes - commonly used classes (1) - String class

String: A string, represented by a pair of "". Sting concept:

​1.String is declared as final and cannot be inherited

​2.String implements the Serializable interface: indicates that the string supports serialization.
​ Implemented the Comparable interface: indicates that String can be compared in size

​3.String internally defines final char[] value for storing string data
4.String: represents an immutable character sequence. Short name: immutability.

体现:1.当对字符串重新赋值时,需要重写指定内存区域,不能使用原有的value赋值。
    2.当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能使用原有的value赋值。
    3.当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有的value赋值。

5. Assign a value to a string by literal method (different from new), and the string value at this time is declared in the string constant pool.

6. Strings with the same content will not be stored in the string constant pool.

The instantiation method of String:

方式一:通过字面量定义的方式(声明在方法区中的字符串常量池中)
方式二:通过new +构造器的方式

Conclusion:
The splicing result of constants and constants is in the constant pool. And there will not be constants with the same content in the constant pool.
As long as one of them is a variable, the result is in the heap
If the concatenated result calls the intern() method, the return value is in the constant pool

Conversion between/between String and basic data types and wrapper classes.

    String -->基本数据类型、包装类:调用包装类的静态方法: **parseXxx(str)**
    基本数据类型、包装类--> string:调用string重载的**valueOf(xxx)**

String and charconversion between

String --> char[]:调用String的**toCharArray( )**
char[] --> String:调用**String的构造器**

Conversion between String and byte[]

编码:String --> byte[]:调用String的getBytes( )
解码: byte[] --> String:调用String的构造器

编码:字符串-->字节(看得懂--->看不懂的二进制数据)
解码:编码的逆过程,字节-->字符串~(看不懂的二进制数据---〉看得懂)

说明:解码时,要求解码使用的字符集必须与编码时使用的字符集一致,否则会出现乱码。

Common methods in the String class:

int Length():返回字符串的长度:return value.Length
char charAt(int index):返回某索引处的字符return value[index]
boolean isEmpty():判断是否是空字符串: return value.Length ==e
String toLowercase():使用默认语言环境,将String中的所有字符转换为小写
string toUppercase():使用默认语言环境,将 String中的所有字符转换为大写
String trim():返回字符串的副本,忽略前导空白和尾部空白
booLean equals(object obj):比较字符串的内容是否相同
boolean equalsIgnoreCase(String anotherString): 与equals方法类似,忽略大小写
String concat(String str):将指定字符串连接到此字符串的结尾。等价于用"+”
int compareTo(String anotherString):比较两个字符串的大小
String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取
string substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串从beginIndex开始,到endIndex结束
boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束
boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始
boolean startsWith(String prefix,int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
boolean contains(CharSequence s):当且仅当此字符串包含指定的char值序列时,返回true
int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引
int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引
int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
注:indexOf和lastIndexOf方法如果未找到都是返回-1
 替换:
String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的。
String replace(CharSequence target,CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
String replaceAll(String regex,String replacement):使用给定的replacenent替换此字符串所有匹配给定的正则表达式的子字符串。
String replaceFirst(String regex,String replacement):使用给定的replacement替换此字符串匹配给定的正则表达式的第一个子字符串。
匹配:
boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。
切片:
String[]split(String regex):根据给定正则表达式的匹配拆分此字符串。
String[]split(String regex,int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。

Guess you like

Origin blog.csdn.net/m0_46450708/article/details/119615629