How to split a string in Java

In Java, we can use String#split() to split a string. String phone = "012-3456789"; // String#split(string regex) accepts regex as the argument String[] output = phone.split("-"); String part1 = output[0]; // 012 String part2 = output[1]; // 3456789 Table of contents 1. Split a string (default) 2. Split a string and retain the delimiter 3. …

Read more

Java StringTokenizer examples

In Java, we use StringTokenizer to split a string into multiple tokens. Note The StringTokenizer is a legacy class, try the split method of String, read this How to split a string in Java. 1. StringTokenizer examples 1.1 By default, the StringTokenizer uses default delimiters: space, tab, newline, carriage-return, and form-feed characters to split a …

Read more