
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to split into a number of sub-strings
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"
- Related Articles
- How to split Python tuples into sub-tuples?
- Java Program to Split a list into Two Halves
- Java Program to format strings into table
- Program to find split a string into the max number of unique substrings in Python
- Program to find number of ways to split array into three subarrays in Python
- How to split a number into digits in R?
- Golang program to split a slice into two halves
- How to split JavaScript Number into individual digits?
- Program to split two strings to make palindrome using Python
- How to split a Java String into tokens with StringTokenizer?
- Java program to split the Even and Odd elements into two different lists
- Java Program to display sub-list of ArrayList
- Write a program in C++ to split two strings to make it a palindrome
- Java Program to Parse and Format a Number into Binary
- Java Program to divide a number into smaller random ints

Advertisements