Visual Basic 6 function "str_replace"

Go back

Below you'll find the source for the Visual Basic 6 function str_replace.

Attribute VB_Name = "modStrReplace"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Option Explicit

' This function replaces a string in another string
' Same syntax as the PHP function 'str_replace'
' See also:
' @param    String  search      The string that must be replaced
' @param    String  treplace    The string to replace to
' @param    String  subject     The original string
' @param    Integer count       The amount of replaces to be done (0 is infinite)
' @return   String              The new string
' @author   Stefan Thoolen <>
Public Function str_replace(search As String, treplace As String, subject As String, Optional count As Integer) As String
    ' I wrote this function but later I found out that replace already existed in VB6
    ' For compatibility reasons I turned this into a wrapper
    If count = 0 Then count = -1
    str_replace = replace(subject, search, treplace, 1, CLng(count))
End Function