Home >Java >javaTutorial >String manipulation techniques in Java
As a widely used programming language, Java has strong processing capabilities in string operations. This article will introduce some commonly used string manipulation techniques in Java to help readers better handle strings.
1. Creation of strings
In Java, we can create string objects through string literals or using constructors. For example:
String str1 = "hello"; String str2 = new String("hello");
When creating a string using string literals, Java will first check whether the string already exists in the string constant pool. If it exists, it will directly return a reference to the string; if If it does not exist, a new string object will be created in the string constant pool and its reference will be returned.
When you use the new
keyword and constructor to create a string, Java will create a new string object in the heap memory.
2. String splicing
The string splicing operation in Java can use the
operator or the concat()
method. For example:
String str1 = "hello"; String str2 = "world"; String str3 = str1 + " " + str2; String str4 = str1.concat(" ").concat(str2);
Using the
operator can easily splice two strings together. If multiple string splicing is involved, multiple
can be used. Splicing. The concat()
method is generally used to concatenate one string to the end of another string.
It should be noted that the string splicing operation will generate a new string object and will not modify the original string object.
3. String interception
In Java, you can use the substring()
method to intercept a string. The parameters of this method include the starting position and end of the interception. Location. For example:
String str = "hello world"; String subStr1 = str.substring(0, 5); String subStr2 = str.substring(6);
The first parameter indicates the starting position of interception (inclusive), and the second parameter indicates the end position of interception (exclusive). When only one parameter is passed in, it means intercepting from that position to the end of the string.
It should be noted that this method does not modify the original string, but returns a new string object.
4. Search and replace strings
In Java, you can use the indexOf()
method to find the first occurrence of a specified substring in a string. You can also use the lastIndexOf()
method to find the last occurrence. For example:
String str = "hello world"; int index1 = str.indexOf("o"); int index2 = str.lastIndexOf("o");
If the substring does not exist in the string, return -1
.
In addition, you can also use the replace()
method in Java to replace substrings in a string. For example:
String str = "hello world"; String newStr = str.replace("world", "Java");
This method will return a new string object, and the original string will not be modified.
5. String separation
In Java, you can use the split()
method to split a string according to the specified delimiter and return the split characters string array. For example:
String str = "apple,orange,banana"; String[] array = str.split(",");
The above code will separate the strings by commas and return an array containing three strings.
6. String conversion
In Java, you can use the valueOf()
method to convert other types of data into strings, for example:
String str1 = String.valueOf(123); String str2 = String.valueOf(3.14); String str3 = String.valueOf(true);
In addition, when converting between strings and other data types, you can use the parseXxx()
method, where Xxx
represents the data type, for example:
int n1 = Integer.parseInt("123"); double n2 = Double.parseDouble("3.14"); boolean n3 = Boolean.parseBoolean("true");
It should be noted that this method can only be used to convert numerical values or Boolean values represented by strings, otherwise an exception will be thrown.
Summary
This article introduces common string manipulation techniques in Java, including string creation, splicing, interception, search, replacement, separation and conversion, etc. I hope readers can flexibly apply these methods in their work and better handle strings.
The above is the detailed content of String manipulation techniques in Java. For more information, please follow other related articles on the PHP Chinese website!