Split string (Java)

Introduction

I tried to summarize various patterns of character division.

Pattern with one delimiter and no second argument

public class Sample {
	public static void main(String[] args) {
		String str = "1,2,3,4";
		String[] strs = str.split(",");
		for (String num : strs) {
			System.out.println(num);
		}
		System.out.println("Number of arrays:"+strs.length);
	}
}

Execution result.


1
2
3
4
Number of arrays:4

Pattern with one delimiter and second argument (when the second argument is greater than 0)

public class Sample {
	public static void main(String[] args) {
		String str = "1,2,3,4";
		String[] strs = str.split(",",3);
		for (String num : strs) {
			System.out.println(num);
		}
		System.out.println("Number of arrays:"+strs.length);
	}
}

Execution result.


1
2
3,4
Number of arrays:3

The second argument specifies the number of elements. In this case, 3 is specified, so the number of elements is 3.

Pattern with one delimiter and second argument (when the second argument is a negative value)

public class Sample {
	public static void main(String[] args) {
		String str = "1,2,3,4,";
		String[] strs = str.split(",",-1);
		for (String num : strs) {
			System.out.println(num);
		}
		System.out.println("Number of arrays:"+strs.length);
	}
}

Execution result.


1
2
3
4

Number of arrays:5

When the second argument is negative, the number of elements in the array is not limited and the empty string at the end of the array is not deleted. In this case, -1 was specified, but the number of elements is not limited because it is a negative value. It also contains an empty string.

When there are multiple delimiters

public class Sample {
	public static void main(String[] args) {
		String str = "1+2=3";
		String[] strs = str.split("[=+]");
		for (String num : strs) {
			System.out.println(num);
		}
		System.out.println("Number of arrays:"+strs.length);
	}
}

Execution result.


1
2
3
Number of arrays:3

To specify multiple delimiters, enter the delimiter in [].

When separating including the delimiter

public class Sample {
	public static void main(String[] args) {
		String str = "1,2,3";
		String[] strs = str.split("(?<=,)");
		for (String num : strs) {
			System.out.println(num);
		}
		System.out.println("Number of arrays:"+strs.length);
	}
}

Execution result.


1,
2,
3
Number of arrays:3

Use (? <=) To include the delimiter.

When separating including multiple delimiters

public class Sample {
	public static void main(String[] args) {
		String str = "1,2a3";
		String[] strs = str.split("(?<=[,a])");
		for (String num : strs) {
			System.out.println(num);
		}
		System.out.println("Number of arrays:"+strs.length);
	}
}

Execution result.


1,
2a
3
Number of arrays:3

To include multiple delimiters, enter the delimiter in [] of (? <= []).

Recommended Posts

Split string (Java)
Java string
[Java] String padding
Java string processing
[Java] split method
Split a string with ". (Dot)" in Java
[Java] String comparison and && and ||
Java string multiple replacement
[Note] Java: String search
[Note] Java: String survey
About Java String class
Java inflexible String class substring
Reflection on Java string manipulation
[Java] About String and StringBuilder
Java
String
Java
[Java] Speed comparison of string concatenation
Various methods of Java String class
Arbitrary string creation code by Java
Studying Java 8 (String Joiner and join)
[Java] Correct comparison of String type
[Java] Divide a character string by a specified character
Java learning (0)
Studying Java ―― 3
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
String puzzle
Java Day 2018
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java (interface)
Java memorandum
☾ Java / Collection
[Java] Data type / string class cheat sheet
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
java framework
Java features