OneBite.Dev - Coding blog in a bite size

split a string by a delimiter in java

Code snippet on how to split a string by a delimiter in java

String[] parts = str.split("[,]");

This code splits a given string, called str, into an array called parts using the delimiter ”,“. The code uses the split method which takes the delimiter as an argument and returns an array of strings after separating the string based on the delimiter. The delimiter used in this example is ”,”, surrounded by brackets. This is necessary to ensure the correct interpretation of the string by the program as split method uses regular expressions. The parts array will contain the parts of the string separated by the delimiter.

java