HELP: Simple recursive function

  • Thread starter Thread starter VB Programmer
  • Start date Start date
V

VB Programmer

I am trying to create a function which strips out ALL occurances of a
particular phrase in a string. It returns the cleaned up string. I gave it
an argument of "1###2###3###4". When it finally gets to the Return
statement it DOES return the correct thing, but by the time it finally
"unravels" it returns nothing. Any ideas????

Private Function StripOutPounds(ByVal strMsg As String) As String
Try
Dim s As String

' strip out every occurance of "###"
If strMsg.IndexOf("###") <> -1 Then ' if an occurance exists
' get everything to the left
s += strMsg.Substring(0, strMsg.IndexOf("###"))
' get everything to the right
s += strMsg.Substring(strMsg.IndexOf("###") + 4)
s = StripOutServerMessage(s)
Else ' return cleaned up message
Return strMsg
End If
End Function
 
Never mind. Instead of "s = StripOutServerMessage(s)" I put "Return
StripOutServerMessage(s)" and it worked!

Thanks.
 
For this particular example, I am wondering why you did not do something
like:

Private Function StripOutPounds(ByVal strMsg As String) As String

Return strMsg.Replace("###","")

End Function

Unless you are trying to use recursion, the Replace method is much simpler.
Perhaps:

strMsg.Replace("#","")

is better.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Hi VB programmer,

Why not

return replace(strMsg,"###","")

is that not something more simple?

Cor
 
* "VB Programmer said:
s += strMsg.Substring(0, strMsg.IndexOf("###"))

Only FYI: In VB.NET, it's recommended to use the '&' or '&=' operator
to concatenate strings.
 
Back
Top