Split a String in Java

This tutorial will teach you how to create a program that will split a string using java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of splitString.java. 2. We will initialize variables in our Main, variable str as a String, and variable elements as a String array.
  1.     String str = "monday,tuesday,wednesday,thursday,friday,saturday,sunday";
  2.    
  3.     String[] elements;
3. To split the string in variable str, we will use the split method of the str with the elements of the String array.
  1.    elements = str.split(",");
As you can see, i have choose to have comma "," on the split keyword so that all the commas in the String will be omitted. The split keyword means to omit those characters in the string. Note: You can change the comma keyword into any characters that you want. 4. To display the text that was split, we will declare a for loop because the string now was an array.
  1.       for (int x = 0; x < elements.length; x++){
  2.       System.out.println(elements[x]);
  3.     }
Output: output Here's the full code of this tutorial:
  1. public class splitString {
  2.   public static void main(String[] args) {
  3.        
  4.     String str = "monday,tuesday,wednesday,thursday,friday,saturday,sunday";
  5.    
  6.     String[] elements;
  7.    
  8.    elements = str.split(",");
  9.    
  10.       for (int x = 0; x < elements.length; x++){
  11.       System.out.println(elements[x]);
  12.     }
  13.  
  14.  
  15.   }
  16. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment