Java's String method has a split
method. By using this method, for example, you can divide a character string at an arbitrary character and generate an array with the divided elements.
example1.java
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
//The string you want to make into an array
String str = "Apple,Orange,banana,watermelon,melon";
//Specify the character to be used for division
String[] strArray = str.split(",");
for(int i = 0; i < strArray.length; i++) {
System.out.println((i + 1) + ":" + strArray[i]);
}
}
}
When you run this source
1: Apple 2: Mandarin orange 3: Banana 4: Watermelon 5: Melon
The result is obtained.
Now, the split
method, but what if the characters used for splitting are consecutive, as in the following example?
example2.java
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
//The string you want to make into an array
String str = "Apple,Orange,,,";
//Specify the character to be used for division
String[] strArray = str.split(",");
for(int i = 0; i < strArray.length; i++) {
System.out.println((i + 1) + ":" + strArray[i]);
}
}
}
When you run this source
1: Apple 2: Mandarin orange 3: 4: 5:
I was expecting to get the result, but it wasn't.
1: Apple 2: Mandarin orange
Has been displayed. In other words, what it means is that the size of the array obtained in ʻexample2.javawill be 2 instead of 5. In the above example, I thought that the size of the array would be 5 if I used the
split` method, but when I actually used it in my business, it was not as expected and I was disappointed.
So how did I get what I expected? In conclusion, specify -1 (negative number) as the second argument of the split
method as shown in the example below.
example3.java
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
//The string you want to make into an array
String str = "Apple,Orange,,,";
//In the second argument-Specify 1
String[] strArray = str.split(",", -1);
for(int i = 0; i < strArray.length; i++) {
System.out.println((i + 1) + ":" + strArray[i]);
}
}
}
By specifying -1 as the second argument,
1: Apple 2: Mandarin orange 3: 4: 5:
I got the result, and I was able to get the size of the array by the number divided by commas.
In solving this problem, I read the Official Document and found two things. I was able to learn.
The first is that the split
method does not split by the string specified by the first argument, but correctly by the ** regular expression ** specified by the first argument.
I was convinced that it would be divided by the specified character string, but I realized that it is possible to divide the character string under more complicated conditions by making full use of regular expressions.
The second is to specify a negative number in the second argument of the split
method when the strings you want to split are consecutive as in the above example.
The second argument of the split
method represents the split threshold. If the second argument is not specified, it will be treated the same as if 0 is specified for the second argument. In this case, if the result of dividing in order is an empty string, it will be discarded.
So in ʻexample2.java`, the size of the split array is now 2 instead of 5.
To prevent this, I found that it is necessary to specify a negative number for the second argument so that the number of divisions is unlimited and the empty string is not discarded.
I was able to reaffirm the importance of referring to the official document when solving this problem. And I was able to realize the importance of using the method after understanding exactly what kind of processing is being performed. I hope this article helps someone.
Recommended Posts