当前位置:网站首页>Common methods of string class
Common methods of string class
2022-07-24 19:18:00 【to be__】
String explain :
String Declare as final, Not to be inherited ;
String Realized Serializebla Interface , Indicates that the string supports serialization ;
String Realized Comparable Interface , indicate String It can be compared in size ;
Assign a value to the string by literal way , At this point, the string value is declared in the string constant pool ( Strings with the same content will not be stored in the string constant pool )
String Represents an immutable sequence of characters , Have no variability
- After reassigning the string constant , You need to re assign values in the specified memory area , Can't be on the original value Modify assignment directly
- When connecting strings , You need to re assign values in the specified memory area , Can't be on the original value Operate directly
- When using replace() Method to replace a character or string , You also need to re assign values in the specified memory area , Can't be on the original value Operate directly
Literal and constructor creation String The difference between
public class StringTest {
@Test
public void test(){
// Literal declaration string :s And s1 The data of is placed in the string constant pool
String s = "hello";
String s1 = "hello";
System.out.println(s==s1); //true The same data value in the constant pool
//new + The way constructors are :s2 And s3 Saved address values , Is the address value corresponding to the data after opening up space in the heap space
String s2 = new String("yes");
String s3 = new String("no");
System.out.println(s2==s3);// Point to different address values in the heap
}
}Connect constants with constants , The result is in the constant pool ; As long as one of the connection operations is a variable , The result is in heap space ;
String Common methods :



//String --> char[] call String Of toCharArray() Method
String s4 = "hello";
char[] c = s4.toCharArray();
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
//char[] --> String call String Constructor
char[] c1 = new char[]{'w','o','r','l','d'};
String s5 = new String(c1);
System.out.println(s5);
//String --> byte[] call String Of getBytes() Method
String s6 = "abc";
byte[] b = s6.getBytes();
System.out.println(Arrays.toString(b)); // [97, 98, 99]
public class String1{
public static void main(String[] args) {
/*
String str = "hello";
1 Intercept out str First character of
char c = str.charAt(0);
char[] data = str.toCharArray(); // take str Converts a string to an array of characters
for (int i = 0; i<data.length; i++){
System.out.println(data[i]);
}
*/
/*
2 Given a string , Judge whether it is composed of numbers
String str = "1234o847290";
if (isNumber(str)){
System.out.println(" A string consists of numbers ");
}else{
System.out.println(" A string consists of non numbers ");
}
}
public static Boolean isNumber(String str){ // Declare the main class method as static, You can use this method directly in the main class , There is no need to declare objects
char[] data = str.toCharArray();
for (int i =0; i<data.length; i++){
if (data[i] <'0' || data[i]>'9'){
return false;
}
}
return true;
*/
/* 3 String comparison
String str1 = "helloworld";
String str2 = "HelloWorld";
System.out.println(str1.equals(str2)); // Distinguish case and compare whether two strings are equal
System.out.println(str1.equalsIgnoreCase(str2)); // Case insensitive comparison of two strings for equality
System.out.println(str2.compareTo(str1)); // Compare the relationship between two strings , The result is greater than 0, Less than 0, Or equal to 0
*/
/*
//4 String truncation
String str = "anbdjhd";
String s1 = str.substring(3); // Truncate from the specified index to the end
String s2 = str.substring(0,4); // Partial intercept [0,4)
System.out.println(s1+"\t"+s2);
*/
/*
//5 String splitting
String str = "hello world you me he";
String result1[] = str.split(" "); // Split with spaces
String result2[] = str.split(""); // Split all strings
System.out.println(result2[6]);
*/
// Determine if it is an empty string strName.isEmpty()
String str = "abdbud";
String str1 = "";
System.out.println(str.isEmpty()); //false Non empty
System.out.println(str1.isEmpty()); //true empty
System.out.println(initcap(str)); // Realize the capitalization of the first letter of a string
}
// Capitalize the first letter , Defined as static type , You can call directly in the main class
public static String initcap(String str){
// First convert the initial letter to uppercase and then add the following string
String temp = str.substring(0,1).toUpperCase() + str.substring(1);
return temp;
}
}边栏推荐
- 关于core文件
- [JVM learning 04] JMM memory model
- Nacos简介和控制台服务安装
- Emergency lighting design of large stadiums and gymnasiums
- 2020-2021 new technology lecture course
- MySQL8.0学习记录20 - Trigger
- 信道状态信息(CSI)共轭相乘去噪法
- mysql排序.按字段值排序
- Channel state information (CSI) conjugate multiplication denoising method
- 2022 Hangzhou Electric Multi school first Dragon Slayer (dfs+ state compression)
猜你喜欢

Nacos introduction and console service installation

Pam4 popular science

How does PostgreSQL decide PG's backup strategy

2022 Hangdian multi school second session 1009 shuangq (Mathematics)
![[resolved] CVC datatype valid. 1.2.1: '' is not a valid value for 'ncname'.](/img/27/ae15e553eba99acf19beb93a3e9939.jpg)
[resolved] CVC datatype valid. 1.2.1: '' is not a valid value for 'ncname'.

Web
![Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]](/img/77/28e8f826c7ee93fedada405c70b82b.png)
Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]

Nacos简介和控制台服务安装

Analysis of dropout principle in deep learning

Common problems of multithreading and concurrent programming (to be continued)
随机推荐
Calendar common methods
Analysis of the basic concept of digital warehouse
Analysis of dropout principle in deep learning
Machine learning_ Data processing and model evaluation
2022 Hangdian multi school second session 1009 shuangq (Mathematics)
Mysql database, de duplication, connection
Chapter 4 compound type
信道状态信息(CSI)共轭相乘去噪法
MySQL8.0学习记录20 - Trigger
asp. Net coree file upload and download example
PCIe link initialization & Training
Equals() method of object class
Pay close attention! List of the latest agenda of 2022 open atom open source Summit
Oneinstack installation and configuration PHP 8.1 and MySQL 8.0-oneinstack site building novice tutorial
MySQL final chapter
Arrays
[understanding of opportunity-48]: Guiguzi - Chapter 7 - collect information in advance, make predictions and implementation plans in advance
多线程与并发编程常见问题(未完待续)
Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack
Decision tree_ ID3_ C4.5_ CART