- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to Split String in Java using Regular Expression?
The split(String regex) method of the String class splits this string around matches of the given regular expression.
This method works in the same way as invoking the method i.e split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array.
Example
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "a d, m, i.n"; String delimiters = "\s+|,\s*|\.\s*"; // analysing the string String[] tokensVal = str.split(delimiters); // prints the number of tokens System.out.println("Count of tokens = " + tokensVal.length); for(String token : tokensVal) { System.out.print(token); } } }
Output
Count of tokens = 5 admin
- Related Articles
- Java Program to split a string using Regular Expression
- How do we use a delimiter to split string in Python regular expression?
- How to split a string using regular expressions in C#?
- How to split on successions of newline characters using Python regular expression?
- Program to match vowels in a string using regular expression in Java
- How to extract each (English) word from a string using regular expression in Java?
- How to print all the characters of a string using regular expression in Java?
- How to match digits using Java Regular Expression (RegEx)
- How to match non-digits using Java Regular Expression (RegEx)
- How to remove white spaces using Java Regular Expression (RegEx)
- How to match at the beginning of string in python using Regular Expression?
- Java Regular expression to check if a string contains alphabet
- Why we should use whole string in Java regular expression
- Moving all special char to the end of the String using Java Regular Expression RegEx)
- How to get first letter of each word using regular expression in Java?

Advertisements