Split String with Dot in Java



In Java, strings are one of the most commonly used data types for storing text. Sometimes, you may need to split a string based on a specific delimiter, such as a dot (.). Java provides powerful string manipulation methods like split().

Split() method 

The split() method of Java String is used to divide a string into an array of substrings. This method takes a string in the form of a regular expression as a parameter, searches the currently existing string with the given pattern, and splits it at every occurrence of the pattern matched.

Split String with Dot (.) in Java

Let's say the following is our string ? 

String str = "This is demo text.This is sample text!";

To split a string with the dot, use the split() method in Java ?

str.split("[.]", 0); 

Example

Below is the Java program to split strings with the dot ?

public class Demo {
    public static void main(String[] args) {
       String str = "This is demo text.This is sample text!";
       String[] res = str.split("[.]", 0);
       for(String myStr: res) {
          System.out.println(myStr);
       }
    }
}

Output

This is demo text
This is sample text!
Updated on: 2024-12-26T20:41:49+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements