Replace In String RRS feed

  • Question

  • User436194884 posted

    I have a series of displays that may, or may not, contain specific characters within a string that I need to replace.

    For example:

    Jones - 05002

    Smith - 09001

    Unger - 10101

    Should be 

    Jones - B002

    Smith - C001

    Unger - D101

    IF string contains "- 09" then replace "- 09" with "- C" elseif string contains "- 05" then replace "- 05" with "- B" etc.

    Wednesday, August 21, 2019 2:37 PM

All replies

  • User-848649084 posted


    You could use classic asp to replace function to full fill your requirement.



    string: Required. The string to be searched
    find: Required. The part of the string that will be replaced

    replacewith: Required. The replacement substring

    start: Optional. Specifies the start position. Default is 1. All characters before the start position will be removed.
    count : Optional. Specifies the number of substitutions to perform.
    Default value is -1, which means make all possible substitutions

    compare: Optional. Specifies the string comparison to use. Default is 0
    Can have one of the following values:

    0 = vbBinaryCompare - Perform a binary comparison
    1 = vbTextCompare - Perform a textual comparison

    Below is the example code:

    <!DOCTYPE html>
    txt1="Jones - 05002"
    txt2="Smith - 09001"
    txt3="Unger - 10101"
    response.write("Before Replace" & "<br><br>")
    response.write(txt1 & "<br />" & txt2 & "<br />" & txt3 & "<br /><br>")
    response.write("After Replace" & "<br><br>")
    response.write(Replace(txt1,"05","B",1,1)& "<br />")
    response.write(Replace(txt2,"09","C",1,1)& "<br />")
    response.write(Replace(txt3,"10","D",1,1)& "<br />")



    Thursday, August 22, 2019 3:28 AM
  • User436194884 posted

    Jalpa, thank you so much for your reply but I couldn't be as prescriptive as there is a single string for each of those that could contain any of the above.

    For example the display is currently <%=rs_loc("pd_01_tch_room")%> and this could be Jones - 05002, Smith - 09001, Unger - 10101 or any other of about 200 combinations where the first two numbers (building number) need to be replaced with a letter to represent that building.

    Thanks again for your reply, sorry if I'm missing it and either way I hope this is helpful.

    Thursday, August 22, 2019 11:00 AM
  • User-848649084 posted

    You need to write code for it. 

    Friday, August 23, 2019 7:38 AM
  • User436194884 posted

    Yes, but that's where I'm looking for help as the only way that I have experience with would result in an extraordinarily long statement with about 200 different combinations of users buildings and rooms.

    IF rs_loc("pd_01_tch_room") = "Jones - 05002" THEN response.write "Jones - B002" Else If rs_loc("pd_01_tch_room") = "Smith - 09001" THEN response.write "Smith - C001" Else If rs_loc("pd_01_tch_room") = "Unger - 10101" THEN response.write "Unger - D101" ..... repeat this for every possible combination

    If I could just replace every instance of the first two numeric characters with the appropriate letter it would be great but am unfamiliar with how that might be accomplished.

    Thanks again for your time, Scott

    Friday, August 23, 2019 10:57 AM
  • User-848649084 posted


    you could use a regular expression to full fill your requirement.

    Wednesday, September 4, 2019 8:15 AM
  • User436194884 posted

    Jalpa, thank you for your time in replying, I just used an incredibly long conditional statement as I needed to get it up. It seems to be flying fine for now and I will look into the earlier post.

    Thanks again!

    Wednesday, September 4, 2019 10:55 AM