Explain Codes LogoExplain Codes Logo

Java string split with "." (dot)

java
string-split
regex
edge-cases
Alex KataevbyAlex Kataev·Aug 10, 2024
TLDR

Need to split a string by a period in Java? Use split("\\."). Remember to escape the dot using double backslashes for accurate partitioning.

// The internets is divided into domains...so let's do some division! String[] parts = "www.google.com".split("\\."); // Output: {"www", "google", "com"}

Delving into the finer regex details

A dot . and a backslash \ aren't merely punctuation in the realm of regular expressions: They have a more profound and nuanced role.

The Dot - A Special Character

In regex, a dot . is a wildcard character, meaning it matches any single character. By adding a backslash \\ before the dot, you tell the interpreter to treat it as a literal.

Limit Argument and Empty Strings

Ever wonder why empty strings appear when you split '.com.'? The second argument, -1, tells split() to include trailing empty strings. Without it, these blanks are lost in the ether.

// Dot com bubble? More like dot com balloon... String[] partsWithTrailing = ".com.".split("\\.", -1); // Output: {"", "com", ""}

Special Characters and Splitting

You'll be thrilled to know that with delimiters like forward slash /, they can simply take a RegExp vacation, no escape needed:

// Slashing prices like a grocery store! String[] partsWithSlash = "grocery/store/items".split("/"); // Output: {"grocery", "store", "items"}

Understanding edge case considerations

In programming, being prepared for edge cases is worth its weight in bitcoins. Let's tackle a few:

Non-Delimiter Strings

Your string lacks a delimiter? Fear not, the result is simply the string itself:

// Nothing to separate...I guess we aren't that different after all String[] noDelimiter = "weareone".split("\\."); // Output: {"weareone"}

Dot-Only Strings

The result of splitting a string of only a '.' or an empty string is quite fascinating:

// Feeling empty on the inside (and the outside)... String[] dotOnly = ".".split("\\.", -1); // Output: {"", ""}

File Extensions Catcher

In the Python world, this is known as the "Indiana Jones" maneuver, where we snatch the filename away just before the split() closes (get it, like the movie?):

// "It belongs in a museum!" - filename, probably String filename = "holygrail.jpg"; String[] fileParts = filename.split("\\.", -1); String nameWithoutExtension = fileParts[0]; // "holygrail"

Approach to blank outcomes

Sometimes, after splitting a string, we need to clean up. Here's how to handle blank results:

No Limit and Negative Limit

With no limit, trailing blanks are left out:

// Skrillex's favorite method: drop the blanks! String[] noLimit = "dub.step...".split("\\."); // Output: {"dub", "step"}

Choosing negative limit, all blanks are included:

// "All are welcome!" - Negative limit, probably String[] negativeLimit = "dub.step...".split("\\.", -1); // Output: {"dub", "step", "", "", ""}

Blank or not, all parts of the parsed string are valuable for understanding the data's structure.