String

String split method

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 27th, 2013
0 52 1 minute read

This is an example of how to split a String. TheString class represents character strings. All string literals in Java programs, such as"abc", are implemented as instances of this class. Splitting a String implies that you should:

  • Create a new String.
  • Use thesplit(String regex) API method of String. This method splits this string around matches of the given regular expression.
  • Usesplit(String regex, int limit) API method of String. This method splits this string around matches of the given regular expression.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;public class StringSplitMethod {public static void main(String[] args) {String s = "Java Code Geeks";String[] temp;String delimiter; delimiter = " ";temp = s.split(delimiter);for (int i =0; i< temp.length ; i++)System.out.println(temp[i]);System.out.println();temp = s.split(delimiter,2);for (int i =0; i< temp.length ; i++)System.out.println(temp[i]);System.out.println();s = "Java.Code.Geeks";delimiter = "\.";temp = s.split(delimiter);for (int i =0; i< temp.length ; i++)System.out.println(temp[i]); }}

Output:

JavaCodeGeeksJavaCode GeeksJavaCodeGeeks

 
This was an example of how to split a String in Java.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: October 27th, 2013
0 52 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.