- 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
Java String split() method example.
The split(String regex, int limit) method of the String class. splits the current 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 does not match any part of the input then the resulting array has just one element, namely 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*"; //analysing the string 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); } //analysing the string with limit as 3 tokensVal = str.split(delimiters, 3); //prints the count of tokens System.out.println("
Count 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 Articles
- Java StringTokenizer and String Split Example.
- Java String compareTo() Method example.
- Java String substring() Method example.
- Java String endsWith() method example.
- Java String equals() method example.
- Java String equalsIgnoreCase() method example.
- Java String format() method example.
- Java String getBytes() method example.
- Java String getChars() method example.
- Java String indexOf() method example.
- Java String intern() method example.
- Java String isEmpty() method example.
- Java String lastIndexOf() method example.
- Java String length() method example.
- Java String replace() method example.

Advertisements