Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Split String in various ways

farenda 2016-05-04 0

Java Split String

In this tutorial we’re going to show how to split string effectively in Java, consider alternative ways and what are differences between them.

String.split(RegExp)

Notice that all String.split() versions invoke Pattern.compile() underneath, so they are not a good fit for tight loops.

String.split(regexp, positive limit)

Limit is applied n-1 times = 3 – 1 = 2 times:

String s = "Stefan Banach";

System.out.print("s.split(\"a\", 3): ");
System.out.println(Arrays.toString(s.split("a", 3)));

Array will be not greater than n, here 3:

s.split("a", 3): [Stef, n B, nach]

String.split(regexp, negative limit)

Non-positive limit = apply as many times as possible:

String s = "Stefan Banach";

System.out.print("s.split(\"a\", -1): ");
System.out.println(Arrays.toString(s.split("a", -1)));

// non-positive = apply as many times as possible:
System.out.print("s.split(\"h\", -1): ");
System.out.println(Arrays.toString(s.split("h", -1)));

Notice empty trailing string in the second split:

s.split("a", -1): [Stef, n B, n, ch]
s.split("h", -1): [Stefan Banac, ]

String.split(regexp, zero limit)

Zero (0) = apply as many times as possible and remove trailing empty strings:

String s = "Stefan Banach";

System.out.print("s.split(\"h\", 0): ");
System.out.println(Arrays.toString(s.split("h", 0)));

Notice no empty trailing string as in s.split(“h”, -1) case:

s.split("h", 0): [Stefan Banac]

String.split(regexp)

It’s the same as limit = 0:

String s = "Stefan Banach";

System.out.print("s.split(\"h\"): ");
System.out.println(Arrays.toString(s.split("h")));
s.split("h"): [Stefan Banac]

Using Pattern.compile(regexp) (for loops)

When you have to split string in a loop using the same regular expression then it’s more efficient to compile the pattern upfront using java.util.regex.Pattern and then use it to split strings, like here:

String s = "Stefan Banach";

Pattern pattern = Pattern.compile("h");

System.out.print("Pattern.compile(\"h\").split(s): ");
// Split in a loop without Pattern.compile() cost:
System.out.println(Arrays.toString(pattern.split(s)));

Result is the same as from String.split(), but more efficient:

Pattern.compile("h").split(s): [Stefan Banac]

Using StringTokenizer

In cases when a string should be split using characters java.util.StringTokenizer may be a good alternative. It allows to scan long strings one token at a time:

String s = "Stefan Banach";

StringTokenizer tokenizer = new StringTokenizer(s, "h");
System.out.print("StringTokenizer(s,\"h\"): ");
System.out.println(Collections.list(tokenizer));

Here result is the same as in s.split(“h”):

StringTokenizer(s,"h"): [Stefan Banac]

References:

  • Pattern API
  • StringTokenizer API
  • Java Collections Tutorial
Share with the World!
Categories Java Tags java, java-basics
Previous: Java PriorityQueue by example
Next: Java Scanner from String File/Path InputStream

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored