We are excited to announce that the IIS.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

Replace In StringRSS

6 replies

Last post Sep 04, 2019 10:55 AM by teach1

  • Replace In String

    Aug 21, 2019 02:37 PM|teach1|LINK

    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.

  • Re: Replace In String

    Aug 22, 2019 03:28 AM|Jalpa Panchal|LINK

    Hi,

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

    Syntax:

    Replace(string,find,replacewith[,start[,count[,compare]]])
    

    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>
    <html>
    <body>
    
    <%
    
    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 />")
    
    
    %>  
    
    </body>
    </html>

    Regards,

    Jalpa

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: Replace In String

    Aug 22, 2019 11:00 AM|teach1|LINK

    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.

  • Re: Replace In String

    Aug 23, 2019 07:38 AM|Jalpa Panchal|LINK

    You need to write code for it. 

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: Replace In String

    Aug 23, 2019 10:57 AM|teach1|LINK

    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

  • Re: Replace In String

    Sep 04, 2019 08:15 AM|Jalpa Panchal|LINK

    Hi,

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

    .NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
  • Re: Replace In String

    Sep 04, 2019 10:55 AM|teach1|LINK

    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!