Home  >  Article  >  Java  >  Use Java's String.split() function to split a string according to a regular expression

Use Java's String.split() function to split a string according to a regular expression

WBOY
WBOYOriginal
2023-07-25 21:07:502234browse

Use Java's String.split() function to split a string according to a regular expression

In Java, to split a string according to a regular expression, you can use the split() method of the String class. This method can split a string according to the specified regular expression, and store the split substrings in a string array and return it. Let's take a look at how to use this function.

First of all, we need to use the basic syntax of the split() method as follows:

String[] strArray = str.split(regex);

Among them, str is the string to be split, and regex is the regular expression used to specify the splitting method. After the function is executed, a string array strArray will be returned, which stores the substrings split according to the regular expression.

Below we use several examples to demonstrate how to use the split() function.

Example 1: Split the string according to commas

Suppose we have a string str with the content "apple,banana,orange", and we want to split it into three substrings according to commas" apple", "banana" and "orange" can be achieved using the following code:

String str = "apple,banana,orange";
String[] strArray = str.split(",");
for (String s : strArray) {
    System.out.println(s);
}

The output result is:

apple
banana
orange

Example 2: Split the string according to multiple delimiters

Sometimes we need to split a string according to multiple different delimiters. In this case, we can use the OR (|) operator of regular expressions. Suppose our string is "apple,banana;orange", we want to split it into three substrings "apple", "banana" and "orange" according to commas and semicolons. This can be achieved using the following code:

String str = "apple,banana;orange";
String[] strArray = str.split(",|;");
for (String s : strArray) {
    System.out.println(s);
}

The output result is:

apple
banana
orange

Example 3: Split the string according to special characters

When using the split() function, you need to pay attention to the escaping of some special characters. For example, if you want to split a string into substrings based on dots, the regular expression would be ".". Suppose we have a string str with the content "www.github.com", and we want to split it into three substrings "www", "github" and "com" according to the period. This can be achieved using the following code:

String str = "www.github.com";
String[] strArray = str.split("\.");
for (String s : strArray) {
    System.out.println(s);
}

The output result is:

www
github
com

Through these examples, we can see that it is very flexible and convenient to use Java's String.split() function to split strings according to regular expressions. If we master the basic syntax and rules of regular expressions, we can easily handle various string splitting needs with the help of the split() function. Hope this article helps you!

The above is the detailed content of Use Java's String.split() function to split a string according to a regular expression. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn