English

Learn Java – How to Split a String?

If you are looking to learn how to split a string in Java, you have come to the right place. String operations are the most common operations when it comes to programming. Java Strings are no different and will be used in almost every program you write.

To split a string in Java, you need to use the String.split() method that is readily available to the Java programmer. Let’s see an example to get a better idea.

String string = “live-coding”

String[] splitted= string.split(“-”);

String splitted1= splitted[0]; //live

String splitted2 = splitted[1]; //coding

The main method that does the job is the split() method. The method takes regex as an argument. Regex is great. However, they can be complex to use. With regex, you need to take care of the escape characters. Let’s learn more about it below.

Escape characters: There are many escape characters in the Java programming language. For example, dot(.) or backslash(), etc.

To handle the escape character, you can either choose to create a special regex for the operation, or you can use Pattern.quote() function to escape the character. Let’s see an example below to understand the whole picture.

String with a dot

As mentioned earlier, you can choose to use the Pattern.quote() method or use the traditional escape method of using a double backslash(). Let’s see both of them live in the code below.

Filename: DotSplit.java

package com.livecoding.test

import java.util.regex.Pattern;

public class DotSplit{
	public static void main(String[] args) {
		String string1= "live.coding.edu";
		String[] result = string1.split(".");

		//You can also use Pattern.quote method if you find escaping escape characters
		//String[] output = string1.split(Pattern.quote("."));
		System.out.println(result[0]);
		System.out.println(result[1]);
		System.out.println(result[2]);
	}
}

Output

live
coding
edu

Alternative Ways

There are many other ways to split the string. One way is to use the StringTokenizer class. StringTokenizer class is a legacy class and should be used with caution.

Putting necessary test cases

Before you want to split a string according to a particular character, it is advisable to test whether it appears i n the string. You can do so by using the contains() method.

The logic goes as below:

If (string.contains(“.”)) {
	//continue with the task
}
else {
	//throw an exception
}

And, that’s the end of the tutorial. If you have any other questions, don’t forget to comment below and let us know.

You can also check on our website for videos about Java. Below are some examples:

  • Lambda expression refactoring (part 1) – Java
  • New Metroid: 2D Game Developing (part 5) – Java

You can also follow some of our broadcasters who program in Java as below:

 chase1263070

 Ankira

Another cool way to find out interesting things about Java is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

2 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

1 month ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago