Friday, 13 April 2018

String Split Example



*
  • Java String split example.
    This Java String split example describes how Java String is split into multiple
    Java String objects.
    */

    public class JavaStringSplitExample{

      public static void main(String args[]){
      /*
      Java String class defines following methods to split Java String object.
      String[] split( String regularExpression )
      Splits the string according to given regular expression.
      String[] split( String reularExpression, int limit )
      Splits the string according to given regular expression. The number of resultant
      substrings by splitting the string is controlled by limit argument.
      */
      /* String to split. */
      String str = “one-two-three”;
      String[] temp;

      /* delimiter */
      String delimiter = “-“;
      /* given string will be split by the argument delimiter provided. */
      temp = str.split(delimiter);
      /* print substrings */
      for(int i =0; i < temp.length ; i++)
        System.out.println(temp[i]);

      /*
      IMPORTANT : Some special characters need to be escaped while providing them as
      delimiters like “.” and “|”.
      */

      System.out.println(“”);
      str = “one.two.three”;
      delimiter = \\.”;
      temp = str.split(delimiter);
      for(int i =0; i < temp.length ; i++)
        System.out.println(temp[i]);

      /*
      Using second argument in the String.split() method, we can control the maximum
      number of substrings generated by splitting a string.
      */
      System.out.println(“”);
      temp = str.split(delimiter,2);
      for(int i =0; i < temp.length ; i++)
        System.out.println(temp[i]);

      }

    }

    /*
    OUTPUT of the above given Java String split Example would be :
    one
    two
    three
    one
    two
    three
    one
    two.three
    */

Related Posts:

  • ArrayList Array List:-ArrayList class uses a dynamic array for storing the elements. Example:-Public Class MyClass { public static void main(str… Read More
  • Interface An interface in java is a blueprint of a class. It has static constants and abstract methods. The interface in java is a mechanism to achieve a… Read More
  • Difference between ArrayList and LinkedList ArrayList LinkedList 1) ArrayList internally uses dynamic array to store the elements. LinkedList internally uses doubly linked list … Read More
  • LinkList (Java) Java Linked List class uses doubly linked list to store the elements. The important points about Java Linked List are: Java Linked List … Read More
  • Recursion –>Recursion is a process in which a method calls itself continuously.  A method in java that calls itself is called recursive me… Read More

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (98) AI (41) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (200) C (77) C# (12) C++ (83) Course (67) Coursera (251) Cybersecurity (25) Data Analysis (3) Data Analytics (3) data management (11) Data Science (149) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (86) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1058) Python Coding Challenge (461) Python Quiz (130) Python Tips (5) Questions (2) R (70) React (6) Scripting (3) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)