# Get all combinations of recursive substring replacement

Hello!
I'm looking for a way to replace a substring within a string, and obtaining all different combinations, for example:

"one leg two legs", and replacing "leg" with "arm" it would return the following strings:

"one leg two legs"
"one arm two legs"
"one leg two arms"
"one arm two arms"

I have made a quite simple routine for this but it fails when replacing "i" with "ie" (infinite recursion because string builds up, pie > piee > pieee > pieeee) and if i fix that by recursing backwards if tails when replacing for example "ch" with "sch" (michael > mischael > misschael > missschael) - im just stuck as hell and trying all night - please help?
###### Who is Participating?

Middle School Assistant TeacherCommented:
Try something like this out:
``````Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim data As String = "one leg two legs three leg eight legs"
Dim find As String = "leg"
Dim replace As String = "arm"

Dim values() As String = data.Split(New String() {find}, StringSplitOptions.None)
Dim combos As Integer = Math.Pow(2, values.Length - 1)

Dim binary As String
Dim maxWidth As Integer = Convert.ToString(combos - 1, 2).Length
Dim combination As New System.Text.StringBuilder
For i As Integer = 0 To combos - 1
combination.Clear()
For j = 0 To values.Length - 2
combination.Append(values(j) & IIf(binary.Substring(j, 1) = "0", find, replace))
Next
combination.Append(values(values.GetUpperBound(0)))
Debug.Print(combination.ToString)
Next
End Sub

End Class
``````

Which produced the following output:
one leg two legs three leg eight legs
one leg two legs three leg eight arms
one leg two legs three arm eight legs
one leg two legs three arm eight arms
one leg two arms three leg eight legs
one leg two arms three leg eight arms
one leg two arms three arm eight legs
one leg two arms three arm eight arms
one arm two legs three leg eight legs
one arm two legs three leg eight arms
one arm two legs three arm eight legs
one arm two legs three arm eight arms
one arm two arms three leg eight legs
one arm two arms three leg eight arms
one arm two arms three arm eight legs
one arm two arms three arm eight arms
0

Commented:
When you replace a string, advance the pointer the length of the string you just replaced with.

What functions are you using? Find? The parameter for find that tells where to start the search would be the pointer. So just increment that by WordYouJustAdded.Length
0

Author Commented:
Thx mate!
0

Author Commented:
Thank you TommySzalapski! Im sure your code would have worked too, however I preferred the second one since it didnt use recursing (brute) but mathematically found all combinations (finesse), thanks a lot for your help though!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.