In this article, we will discuss how to extract all possible substrings from a string in Java
This is a frequently asked interview question.
Let’s look at the below example, to find all substrings in a string.
package com.topjavatutorial; public class AllSubstrings { public static void main(String[] args) { String input = "abcd"; allSubStrings(input); } private static void allSubStrings(String value) { String substring = ""; for (int i = 1; i < value.length(); i++) { for (int j = 0; j <= value.length() - i; j++) { substring = value.substring(j, i + j); System.out.println(substring); } } } }
Output :
a
b
c
d
ab
bc
cd
abc
bcd
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post