Forum Discussion

rsromeo's avatar
rsromeo
Channel Surfer
15 years ago

REPLACE SUBSTRING FUNCTION

Hi -

Does anyone know if Brightscript has a replace function to replace s substring of text?

for example, I would like to do something like this...

string = "this is a file name.mp4"
newstring = replace(string," ","%20")
  • You can find it in generalUtils.brs in the videoplayer example.

    '******************************************************
    'Replace substrings in a string. Return new string
    '******************************************************
    Function strReplace(basestr As String, oldsub As String, newsub As String) As String
    newstr = ""

    i = 1
    while i <= Len(basestr)
    x = Instr(i, basestr, oldsub)
    if x = 0 then
    newstr = newstr + Mid(basestr, i)
    exit while
    endif

    if x > i then
    newstr = newstr + Mid(basestr, i, x-i)
    i = x
    endif

    newstr = newstr + newsub
    i = i + Len(oldsub)
    end while

    return newstr
    End Function
  • Also, for your specific example, you might be interested in roUrlTransfer.Escape( string ).

    String Escape(String text)
    URL encode the specified string using CURL.
  • "evilmax17" wrote:
    Also, for your specific example, you might be interested in roUrlTransfer.Escape( string ).

    String Escape(String text)
    URL encode the specified string using CURL.

    And/Or UrlEncode()...