Java Program to split into a number of sub-strings

JavaCampus InterviewServer Side ProgrammingProgramming

In this article, we will understand how to splitting into a number of sub-strings. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). A part or a subset of string is called substring.

Below is a demonstration of the same −

Suppose our input is

Input string: JVM

The desired output would be

The substring list printed as an ArrayList :
[J, JV, JVM, V, VM, M]

The sub-strings after splitting is:
(1) "J"
(2) "JV"
(3) "JVM"
(4) "V"
(5) "VM"
(6) "M"

Algorithm

Step 1 - START
Step 2 - Declare a string namely input_string, an array list namely string_list.
Step 3 - Define the values.
Step 4 - Iterate through the length of the string using two nested loops, and add every character from every string to another list. This is the result to be displayed on console.
Step 5 - To print this substring as ArrayList, initialize a counter, and iterate through the result and increment the counter after every iteration. This gives number of substrings in the string.
Step 6 - Display the result
Step 7 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.io.*;
import java.util.ArrayList;
public class SubString {
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string = "JVM";
      System.out.println("The string is defined as: " +input_string);
      int i, j;
      int string_length = input_string.length();
      ArrayList<String> string_list = new ArrayList<String>();
      for (i = 0; i < string_length; i++) {
         for (j = i + 1; j <= string_length; j++) {
            string_list.add(input_string.substring(i, j));
         }
      }
      ArrayList<String> result = string_list;
      System.out.println( "\nThe substring list printed as an ArrayList : ");
      System.out.println(result);
      System.out.println( "\nThe sub-strings after splitting is: ");
      int count = 1;
      for (String it : result) {
         System.out.println("(" + count + ") \"" + it + "\"");
         count++;
      }
   }
}

Output

Required packages have been imported
The string is defined as: JVM

The substring list printed as an ArrayList :
[J, JV, JVM, V, VM, M]

The sub-strings after splitting is:
(1) "J"
(2) "JV"
(3) "JVM"
(4) "V"
(5) "VM"
(6) "M"

Example 2

Here, we encapsulate the operations into functions exhibiting object oriented programming.

import java.io.*;
import java.util.ArrayList;
public class SubString {
   public static ArrayList<String> split_string(String input_string) {
      int i, j;
      int string_length = input_string.length();
      ArrayList<String> string_list = new ArrayList<String>();
      for (i = 0; i < string_length; i++) {
         for (j = i + 1; j <= string_length; j++) {
            string_list.add(input_string.substring(i, j));
         }
      }
      return string_list;
   }
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string = "JVM";
      System.out.println("The string is defined as: " +input_string);
      ArrayList<String> string_list = SubString.split_string(input_string);
      System.out.println( "\nThe substring list printed as an ArrayList : ");
      System.out.println(string_list);
      System.out.println( "\nThe sub-strings after splitting is: ");
      int count = 1;
      for (String it : string_list) {
         System.out.println("(" + count + ") \"" + it + "\"");
         count++;
      }
   }
}

Output

Required packages have been imported
The string is defined as: JVM

The substring list printed as an ArrayList :
[J, JV, JVM, V, VM, M]

The sub-strings after splitting is:
(1) "J"
(2) "JV"
(3) "JVM"
(4) "V"
(5) "VM"
(6) "M"
raja
Updated on 29-Mar-2022 12:09:04

Advertisements