Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I find all consecutive substrings in a string given two indices


What I have tried:

def all_possible_substring(string, start, end){
  for ( i : [start:end+1] ){ // from index start to end
    for ( j : [i:end+1] ){ // from index i to end
      println( string[i:j]) // splice the string from i,j and print
    }
  }
}
all_possible_substring( "abcdef" , 0, 2) // test it off
Posted
Updated 6-Nov-17 0:49am
Comments
CodingLover 6-Nov-17 3:06am    
Can you post an example?
Suvendu Shekhar Giri 6-Nov-17 5:52am    
Yes, please post one example of both input and output what you need.

1 solution

It is easier than that. Your end index must never be greater than the end index of your string, so that is the value to check in your loop. Something like :
def all_possible_substring(string, start, end){
  WHILE (end < string.length - 1)
      println( string[start:end])
      start += 1
      end += 1
  }
}
all_possible_substring( "abcdef" , 0, 2) // test it off


Yes, I know this is not PHP, but the logic is the same.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900