#include once "window9.bi"

Function ReplaceString(ByRef sText As USTRING,byRef sSearchText As USTRING,_
						byRef sReplaceText As USTRING,ByVal iStartPos As long=1,_
						ByVal iParamSearch As long=0, ByVal iRegisterParam As long=0) As USTRING Export
	
	Dim As USTRING sTempString , sReturnString = sText
	
	Dim As long iPosSearch , iPosSearchNext = iStartPos
	
	Do
		
		If iRegisterParam=0 Then
			
			Dim As USTRING RS = sReturnString , SS = sSearchText
			
			#Ifdef __FB_WIN32__
				
				#ifdef UNICODE
					CharLower(cast(any ptr , *RS)):CharLower(cast(any ptr , *SS))
				#else
					CharLower(cast(any ptr , strptr(RS))):CharLower(cast(any ptr , strptr(SS)))
				#EndIf
				
			#else
			
				RS = Lcase(wstr(RS))
				
				SS = Lcase(wstr(SS))				
				
			#endif
		
			iPosSearch = InStr( iPosSearchNext , RS , SS )
		
		Else
			
			iPosSearch = InStr( iPosSearchNext , sReturnString , sSearchText )
		
		end if
		
		If iPosSearch<>0 Then
			
			sTempString = Mid( sReturnString , 1 , iPosSearch - 1 ) & sReplaceText & Mid( sReturnString , iPosSearch + Len(sSearchText) )
			
			If iParamSearch=1 Then
			
				sReturnString = sTempString
			
				iPosSearchNext = iPosSearch + Len(sReplaceText)
			
			Else
				
				Return sTempString
			
			End If
		
		Else
			
			Return sReturnString
		
		End If
	
	Loop
	
End Function
