Trying to replace a Variable in URL with value

Hallo,

I have a URL:

inputURL = “https:/cfb790-2ad9-550b-bb0d-4e9cc5456758/sr/?objectdefinitionids=[%22DSDAU%22]&properties={%22333%22%3A[%22Number%22]%2C%2228%22%3A[%22NewNumber%22]%2C%22property_remark%22%3A[%22PDF%2FAZSDODEL*%22]}&propertysort=property_creation_date&ascending=false&showdetails=false”

And I replace it with the value from my variable which is defined string.

UpdatedURL = System.Text.RegularExpressions.Regex.Replace(inputURL, “NewNumber” , theNumber)

here the value of

theNumber = 32056489

but my UpdatedURL comes as

“https:/cfb790-2ad9-550b-bb0d-4e9cc5456758/sr/?objectdefinitionids=[%22DSDAU%22]&properties={%22333%22%3A[%22Number%22]%2C%2228%22%3A[%223%22]%2C%22property_remark%22%3A[%22PDF%2FAZSDODEL*%22]}&propertysort=property_creation_date&ascending=false&showdetails=false”

How do I replace it with complete number?

It looks like the issue is that theNumber is not being correctly inserted into the URL, and instead, the number is being encoded as [%223%22] (which represents 3 in URL encoding). To properly replace the value in the URL with the complete theNumber, you should ensure that the number is not encoded during the replacement process.

Here’s the approach to replace the string "NewNumber" with the actual number 32056489:

Dim inputURL As String = "https:/cfb790-2ad9-550b-bb0d-4e9cc5456758/sr/?objectdefinitionids=[%22DSDAU%22]&properties={%22333%22%3A[%22Number%22]%2C%2228%22%3A[%22NewNumber%22]%2C%22property_remark%22%3A[%22PDF%2FAZSDODEL*%22]}&propertysort=property_creation_date&ascending=false&showdetails=false"
Dim theNumber As String = "32056489"

' Perform the replacement
Dim UpdatedURL As String = System.Text.RegularExpressions.Regex.Replace(inputURL, "NewNumber", theNumber)

Console.WriteLine(UpdatedURL)

Explanation:

  1. Regex.Replace: The expression System.Text.RegularExpressions.Regex.Replace(inputURL, "NewNumber", theNumber) will search for the exact string "NewNumber" and replace it with the value of theNumber ("32056489" in this case).
  2. Ensure theNumber is a string: If theNumber is already a number, you may want to convert it to a string before replacing, like so:
    Dim theNumber As String = 32056489.ToString()
    

With this approach, the URL will properly be updated to:

https:/cfb790-2ad9-550b-bb0d-4e9cc5456758/sr/?objectdefinitionids=[%22DSDAU%22]&properties={%22333%22%3A[%22Number%22]%2C%2228%22%3A[%2232056489%22]%2C%22property_remark%22%3A[%22PDF%2FAZSDODEL*%22]}&propertysort=property_creation_date&ascending=false&showdetails=false

This should solve the issue and correctly replace "NewNumber" with the full number.

1 Like

Hi,

In my environment, your expression works well.

Can you check content of each variable using Breakpoint in debug mode?

Regards,

1 Like

@parnalmahavir.patni,

Isn’t the normal String.Replace working?

Try like this

UpdatedURL = inputURL.Replace("NewNumber", theNumber)

Thank you everyone.
The error was in the input text file. Which was read char by char and hence it was replacing only one number. I corrected it.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.