Sunday 13, Apr 2025
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

01.package com.javatutorialcorner.javastring;
02. 
03.public class SplitString {
04. 
05.    public static void main(String[] args) {
06.        String input1 = "This is Java string example program";
07.        String input2 = "a-b-c-d-e-f";
08. 
09.        String[] result1 = input1.split(" ");
10.        for (int i = 0; i < result1.length; i++) {
11.            System.out.println(result1[i]);
12.        }
13. 
14.        System.out.println("-------------------");
15.        String[] result2 = input2.split("-");
16.        for (String output : result2) {
17.            System.out.println(output);
18.        }
19. 
20.    }
21. 
22.}

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

Related Posts:

  • How to find last occurrence of substring inside stringFollowing example we are going to see about “how to find the last occurrence position of substring inside string using  lastIndexOf(String str) method in Java. Sample Programpackage com.javatutorialcorner.javastring;publ… Read More
  • How to reverse the String using StringBufferFollowing example shows how to reverse the String using StringBuffer. Sample Programpackage com.javatutorialcorner.javastring;public class StringReverse { public static void main(String[] args) { String inputString … Read More
  • How to make read-only file using Java Following example shows how to make file read-only using file.setReadOnly() method in Java. setReadOnlypublic boolean setReadOnly() Marks the file or directory named by this abstract pathname so that only read operations a… Read More
  • How to get File Last modified Date in Java Following example shows how to get file last modified date using file.lastModified() method in Java. lastModified public long lastModified()  Returns the time that the file denoted by this abstract pathname was last mo… Read More
  • Remove the particular character from string Following example shows, how to remove particular character from string. Sample Program package com.javatutorialcorner.javastring; public class RemoveCharacter { public static void main(String[] args) { String … Read More
  • 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