

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 a string in Java?
Java provides a split() methodology you'll be able to split the string around matches of the given regular expression.
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.
If the expression doesn't match any a part of the input then the ensuing array has the only 1part, specifically this string.
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*"; String[] tokensVal = str.split(delimiters); // prints the count of tokens System.out.println("Count of tokens = " + tokensVal.length); for(String token : tokensVal) { System.out.print(token); } tokensVal = str.split(delimiters, 3); System.out.println("\nCount of tokens = " + tokensVal.length); for(String token : tokensVal) { System.out.print(token); } } }
Output
Count of tokens = 5 admin Count of tokens = 3 adm, i.n
- Related Questions & Answers
- Split a string in Java
- How to split a string in Python
- How to split a string in Golang?
- How to split a Java String into tokens with StringTokenizer?
- Java Program to split a string with dot
- Java program to split and join a string
- How to Split String in Java using Regular Expression?
- How to split a string in Lua programming?
- How to split a string with a string delimiter in C#?
- Java String split() method example.
- Split a string around a particular match in Java
- Java Program to split a string using Regular Expression
- Split String with Dot (.) in Java
- Split String with Comma (,) in Java
- How to use Java String.split() method to split a string by dot?
Advertisements