GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java split string - splitting strings in Java Java split stringlast modified February 20, 2024
Java split string tutorial shows how to split strings in Java. We use
The
The method returns an array of split strings.
In addition to the Split phone numberIn the first example, we split a phone number. Main.java
import java.util.Arrays;
void main() {
String phoneNumber = "202-555-0154";
String[] output = phoneNumber.split("-");
Arrays.stream(output).forEach(part -> System.out.println(part));
}
Phone numbers are often separated with the dash (-) character. The program
splits the number into numerical values with String phoneNumber = "202-555-0154"; This is the phone number. String[] output = phoneNumber.split("-");
We split the string by the dash character; the Arrays.stream(output).forEach(part -> System.out.println(part)); We show the split parts to the console. 202 555 0154 Split string with dot character
A dot character has a special meaning in regular expression syntax. To split
a string by a dot, we need to escape it or use Main.java
import java.util.Arrays;
import java.util.regex.Pattern;
void main() {
String address = "127.0.0.1";
// String[] output = address.split("\\.");
String[] output = address.split(Pattern.quote("."));
Arrays.stream(output).forEach(part -> System.out.println(part));
}
The program splits an IP address. Splitting string with limit
The Main.java
import java.util.Arrays;
void main() {
String names = "Jane-Paul-Ferenc-David-Robert-Julia";
String[] output = names.split("-", 4);
Arrays.stream(output).forEach(System.out::println);
}
The program splits the Jane Paul Ferenc David-Robert-Julia Splitting and trimming stringsWe often need to remove the white space characters around the strings. Main.java
import java.util.Arrays;
void main() {
String input = " wood, falcon\t, sky, forest\n";
String[] output = input.trim().split("\\s*,\\s*");
Arrays.stream(output).forEach(System.out::println);
}
To remove the white spaces, we use String output = input.trim().split("\\s*,\\s*");
The regular expression splits the input string by a comma character, which might have any number of white spaces before or after the comma. Split string with Pattern
We can split strings with Main.java
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
void main() {
String phoneNumber = "202-555-0154";
List<String> output = Pattern.compile("-")
.splitAsStream(phoneNumber)
.collect(Collectors.toList());
output.forEach(System.out::println);
}
The example splits a phone number with List<String> output = Pattern.compile("-")
.splitAsStream(phoneNumber)
.collect(Collectors.toList());
We compile a regular expression containing the character we use for
splitting. On the compiled expression, we call Split string with Guava's Splitter
The following example uses Google's Guava library to split a string.
It uses the <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
We need to include Guava's dependency. SplitStringGuavaEx.java
package com.zetcode;
import com.google.common.base.Splitter;
public class SplitStringGuavaEx {
public static void main(String[] args) {
var input = " falcon, \t\tforest\t, \t\t, moderate, sky\n";
var result = Splitter.on(',')
.trimResults()
.omitEmptyStrings()
.splitToList(input);
result.forEach(System.out::println);
}
}
The program splits a string with Guava's var result = Splitter.on(',')
.trimResults()
.omitEmptyStrings()
.splitToList(input);
We extract the non-overlapping substrings from an input string with SourceJava String - language reference In this article we have shown how to split strings in Java. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |