BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Pattern split() Method With Examples

Last Updated: September 22, 2022 by Chaitanya Singh | Filed Under: java

The split() method of Pattern class is used to split a string based on the specified pattern. Similar to java string split method, this method also accepts regular expression as delimiter.

The Pattern class belongs to java.util.regex package, you need to import java.util.regex.Pattern package to use this class.

There are two variants of the Java Pattern split() method:

  • String[] split(CharSequence p): It breaks the string based on the specified pattern p, It returns all a string array that contains all the substring produced after split.
  • String[] split(CharSequence p, int limit): This limits the number of substrings produced by pattern split() method. Unlike the first variant that returns all the substrings, it only returns the limited substrings specified by the limit argument.

Example 1: Pattern.split() when no limit is defined

import java.util.regex.Pattern;
public class JavaExample {
  public static void main(String[] args) {

    //delimiter dot(.) is specified as pattern
    //Use the double backslash before dot as escape sequence
    Pattern p = Pattern.compile("\\.");
    String[] strArray = p.split("Text1.Text2.Text3");
    for(String s:strArray)
    {
      System.out.println(s);
    }
  }
}

Output:

Java pattern split method

Example 2: Pattern.split() with limit

Here, we are specifying the limit in the split() method. This will restrict the number of substrings returned from this method.

import java.util.regex.Pattern;
public class JavaExample {
  public static void main(String[] args) {
    Pattern p = Pattern.compile("/");

    //limit is specified as 2
    String[] strArray = p.split("Text1/Text2/Text3", 2);
    for(String s:strArray)
    {
      System.out.println(s);
    }
  }
}

Output: As you can see, only two substrings are returned after split because the limit argument is 2.

Split with limit example output
❮ Java StringSplit by Comma ❯

Top Related Articles:

  1. Java String substring() Method with examples
  2. Convert Comma Separated String to HashSet in Java
  3. Java StringBuilder charAt()
  4. Split String by Newline in Java
  5. Split String by Pipe Character ( | ) in Java

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap