The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to split a string in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
How to split a string in java Posted: Sep 18, 2015 2:29 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: How to split a string in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

#1: Java Program to split a String using String Split() method:

  1. package com.instanceofjava;
  2.  
  3. class SplitString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance-of-Java";
  7.   String strarray[]=str.split("-");
  8.  
  9.     for (int i = 0; i < strarray.length; i++) {
  10.         System.out.println(strarray[i]);
  11.     }
  12.    
  13. }
  14. }
Output:
  1. Instance
  2. of
  3. Java

#2: Java Program to split a String using String Split() method and use for each loop to display
  1. package com.instanceofjava;
  2.  
  3. class SplitString{
  4.  
  5. public static void main(String [] args){ 

  6.   
  7.     String str = "Instance-of-Java";
  8.     String strarray[]=str.split("-");
  9.  
  10.     for (String string : strarray) {
  11.         System.out.println(string);
  12.     }
  13.  
  14. }
  15. }
Output:
  1. Instance
  2. of
  3. Java



#3: Java Program to split a String using String Split() method: Before splitting check whether deliminator is there or not
  1. package com.instanceofjava;
  2.  
  3. class SplitString{
  4.  
  5. public static void main(String [] args){ 

  6.   
  7.     String str = "Instance-of-Java";
  8.    if(str.contains("-")){
  9.  
  10.     String strarray[]=str.split("-");
  11.  
  12.     for (String string : strarray) {
  13.         System.out.println(string);
  14.     }
  15.  
  16.     }
  17.     else{
  18.         System.out.println("String does not conatain specified char so splitting is not possible");
  19.     }
  20.  
  21. }
  22. }
Output:
  1. Instance
  2. of
  3. Java

#4: Java Program to split a String using String Split() method: Before splitting check whether deliminator is there or not
  1. package com.instanceofjava;
  2.  
  3. class SplitString{
  4.  
  5. public static void main(String [] args){ 

  6.   
  7.     String str = "Instance-of-Java";
  8.    if(str.contains(".")){
  9.  
  10.     String strarray[]=str.split("-");
  11.  
  12.     for (String string : strarray) {
  13.         System.out.println(string);
  14.     }
  15.  
  16.     }
  17.     else{
  18.         System.out.println("String does not contains specified char so splitting is not possible");
  19.     }
  20.  
  21. }
  22. }
Output:
  1. String does not contains specified char so splitting is not possible

Read: How to split a string in java

Topic: Low Latency in Java 8 Previous Topic   Next Topic Topic: Resource Efficiency vs. Flow Efficiency, Part 1: Seeing Your System

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use