How to replace characters in strings?

Many of you asked about a solution for changing characters in strings, so we made a simple example for that:

! ------------------------------------------------------------------------------
! Data input
! ------------------------------------------------------------------------------
_charToReplace = "_"
_newChar = "-"

dim _samples[]
    _samples[1] = "A_b_c"
    _samples[2] = "gfhr_er_mkmk"
    _samples[3] = "wert_nkn-kd_dfsf"
    _samples[4] = ""
    _samples[5] = "_"
    _samples[6] = "_asd-"
    _samples[7] = "1900-11-23"
    _samples[8] = "asDFghjkl"
    _samples[9] = "_q_w_e-r_t_z_u-i_o_p-"

! ------------------------------------------------------------------------------
! Changing strings in a loop
! ------------------------------------------------------------------------------

for idx = 1 to vardim1(_samples)

    _currStr = _samples[idx]   ! storing the current string
    _newStr = ""     ! intialize a new empty string
    _i = 1000        ! creating a "guard" for the while, set the max number of runs

    while _currStr <> "" and _i > 0 do
        _n = STRSTR (_currStr, _charToReplace, 1)   ! check the script for _charToReplace

        if _n > 0 then	
            ! _charToReplace is first found in position _n

            ! add the first part of _currStr to _newStr, plus add _newChar
            _newStr = _newStr + STRSUB (_currStr, 1, _n - 1) + _newChar

            ! get a new _currStr from the chars after position _n
            _currStr = STRSUB (_currStr, _n + 1, STRLEN(_currStr) - _n)	 
        else
            ! if _charToReplace is not found then add _currStr to _newStr
            _newStr = _newStr + _currStr
            ! close the loop with changing _currStr to empty string
            _currStr = ""
        endif

        _i = _i - 1	! decrease the "guard" variable in each run
    endwhile
    text2 0, -idx, "new: " + _newStr
next idx