- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Demonstrate the usage of the Pattern.split() method in Java
The specified input sequence can be split around a particular match for a pattern using the java.util.regex.Pattern.split() method. This method has a single parameter i.e. the input sequence to split and it returns the string array obtained by splitting the input sequence around a particular match for a pattern.
A program that demonstrates the method Pattern.split() in Java regular expressions is given as follows:
Example
import java.util.regex.Pattern; public class Demo { public static void main(String[] args) { String regex = "_"; String input = "Oranges_are_orange"; System.out.println("Regex: " + regex); System.out.println("Input: " + input); Pattern p = Pattern.compile(regex); String[] str = p.split(input); System.out.println("
The split input is:"); for (String s : str) { System.out.println(s); } } }
Output
Regex: _ Input: Oranges_are_orange The split input is: Oranges are orange
Now let us understand the above program.
The regex and the input values are printed. Then the input sequence is split around the regex value using the Pattern.split() method. The split input is printed. A code snippet which demonstrates this is as follows:
String regex = "_"; String input = "Oranges_are_orange"; System.out.println("Regex: " + regex); System.out.println("Input: " + input); Pattern p = Pattern.compile(regex); String[] str = p.split(input); System.out.println("
The split input is:"); for(String s : str) { System.out.println(s); }
- Related Articles
- Demonstrate the clone() method in Java
- Explain the usage of the split() method of the String class in Java.
- Explain the usage of the valueOf() method of the String class in Java
- Usage of element() method of Queues in Java
- C program to demonstrate usage of variable-length arrays
- Write the usage of split() method in javascript?
- What is the usage of join() method in JavaScript?
- What is the usage of every() method in JavaScript?
- What is the usage of fill() method in JavaScript?
- What is the usage of copyWithin() method in JavaScript?
- What is the usage of some() method in JavaScript?
- What is the usage of Array.Every() method in JavaScript?
- Demonstrate getting the immediate superclass information in Java
- Demonstrate Static Import in Java
- Demonstrate thread priorities in Java

Advertisements