当前位置:网站首页>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;
}
}边栏推荐
- Modelarts, Pangu big model, ModelBox... Detailed explanation of Huawei cloud AI development production line
- Oneinstack installation and configuration PHP 8.1 and MySQL 8.0-oneinstack site building novice tutorial
- Leetcode652 finding duplicate subtrees
- Interceptors and filters
- LSTM and Gru of RNN_ Attention mechanism
- Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]
- Easily learn pytoch transfer learning to realize surface defect inspection
- Meshlab & PCL ISS key points
- How to convert the world coordinates of unity's camera into view matrix
- 原反补及大小端
猜你喜欢

Clion configuring WSL tool chain

文献阅读:GoPose 3D Human Pose Estimation Using WiFi

asp. Net coree file upload and download example

Techempower web framework performance test 21st round results release --asp Net core continue to move forward

Pay close attention! List of the latest agenda of 2022 open atom open source Summit
![[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'.

Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack

拿捏C指针

FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)

Hidden Markov model HMM
随机推荐
asp. Net coree file upload and download example
About core files
Environment preparation of Nacos configuration center
Convolution neural network receptive field calculation Guide
Detailed explanation of the relationship between MySQL tables
Wireshark simple filter rule
Clion configuring WSL tool chain
Math
Channel state information (CSI) conjugate multiplication denoising method
【JVM学习03】类加载与字节码技术
Those gods on Zhihu reply
Sequences, time series and prediction in tessorflow quizs on coursera (II)
Feature extraction tool transformer Bert
多线程与并发编程常见问题(未完待续)
Thread theory knowledge
【JVM学习04】JMM内存模型
Decision tree_ ID3_ C4.5_ CART
2022 Summer Games of Hangzhou electric power multiple schools 1012aalice and Bob (game theory)
On dynamic application of binary array
pyhanlp安装教程