Firstly, I'll start off by saying English is not my first language so I apologize for any poor explanations.
I want to know how to get every single substring of a String with so many different orders. Before you tell me this question has been asked before, I want to say that almost every code implementation of this task I see does not include duplicates. But Say I had a string "enviroment" and I wanted every single substring including "ment", "met", "ten", "net","note", "more" e.t.c e.t.c how would I acheive this??
This is the function I wrote.
public static ArrayList<String> getAllSubstringsOfAString(String inputString)
{
ArrayList<String> allSubstrings = new ArrayList<String>();
String sub;
for(int i = 0; i < inputString.length(); i++)
{
for (int j = 1; j <= inputString.length() - i; j++)
{
sub = inputString.substring(i , i + j);
allSubstrings.add(sub);
}
}
return allSubstrings;
}
When I run this function
public static void main(String[] args) throws IOException {
ArrayList<String> allSubStrings = getAllSubstringsOfAString("environment");
for (String allSubString : allSubStrings) {
System.out.println(allSubString);
}
it prints this out
e
en
env
envi
envir
enviro
environ
environm
environme
environmen
environment
n
nv
nvi
nvir
nviro
nviron
nvironm
nvironme
nvironmen
nvironment
v
vi
vir
viro
viron
vironm
vironme
vironmen
vironment
i
ir
iro
iron
ironm
ironme
ironmen
ironment
r
ro
ron
ronm
ronme
ronmen
ronment
o
on
onm
onme
onmen
onment
n
nm
nme
nmen
nment
m
me
men
ment
e
en
ent
n
nt
t
Which is only a small part of what I want. I want the function to be able to get substrings in every order. For example, If I wanted it to include Strings like "net", "ten", "never" e.t.c, as they are all substrings of the word "environment". What changes do I have to make on my function to attain this?
Also, as I am a Java beginner, I would like to know If my code is well written and what changes I can make to my code to make it perform better and look better, and to follow common Java coding conventions.
Thanks in advance