We are moved to new domain
Click -> www.ehowtonow.com
Friday, 8 May 2015

How to Split the String in Java

Following Example shows how to split the string using str.split(String pattern) method in Java.

Sample Program

package com.javatutorialcorner.javastring;

public class SplitString {

public static void main(String[] args) {
String input1 = "This is Java string example program";
String input2 = "a-b-c-d-e-f";

String[] result1 = input1.split(" ");
for (int i = 0; i < result1.length; i++) {
System.out.println(result1[i]);
}

System.out.println("-------------------");
String[] result2 = input2.split("-");
for (String output : result2) {
System.out.println(output);
}

}

}


Result


The above code will produce the following result.



This
is
Java
string
example
program
-------------------
a
b
c
d
e
f

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to Split the String in Java Rating: 5 Reviewed By: eHowToNow