-3

So I don't know a lot of c#, I have a javascript background.

I got a string.

String Url = Adress + "?station_ids=1322&observation_source=3&starttime=[date]"

And the "value" (number) of observation_source=is what I want to change. For example to [ObsSource] so I get a string like this:

String Url = Adress + "?station_ids=1322&observation_source=[ObsSource]&starttime=[date]"

I want to do this so I can replace "[ObsSource]" with my own value later to scan different "observation sources"

The observation_source value can be anything between 1 and 100. What I imagine is possible to is to replace from "observation_source" to the next "?" but I don't know where to start.

Is there regex in c#?

Edit: To clarify, the "?station_ids=1322&observation_source=[ObsSource]&starttime=[date]" is not my own making. I get these URLs from a JSON object someone else made. I can't make changes to how the string looks from the sender, just what I do with it once i have it.

4
  • It looks like the String.Format Method would be useful to you. And yes, C# does have regexes. Commented Oct 26, 2016 at 9:28
  • See the duplicate for how to parse and modify query strings. Also, read How to Ask and share your research. Commented Oct 26, 2016 at 9:33
  • It might seem like an unnecessary step, but i have 180 of these strings, and my goal is just to find out how to replace a specific unknown amount of characters in a string. Currently reading up on the duplicate thread, thank you Commented Oct 26, 2016 at 9:36
  • Yes, there are regexps in C#. Please google for "c# regex". Commented Oct 26, 2016 at 14:48

2 Answers 2

1
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"observation_source=(\d+)");
string url2 = re.Replace(Url, (m) =>
{
  return m.Value.Replace(m.Groups[1].Value, "[ObsSource]");
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, Seems to work perfectly! Does "(\d+)" mean any number near eachother? As in, would it break if the number for some reason is 1000001?
@NachoDawg In .NET, \d in a regex means a Unicode digit. You could use [0-9] instead to restrict it to the characters "0" to "9", or use RegexOptions.ECMAScript to make it behave as an ECMAScript regex. The + means "one-or-more consecutive occurrences".
-1

Try this:

        string address = "www.google.com/";
        string url = address + "?station_ids=1322&observation_source=3&starttime=[date]";

        var queryStrings = HttpUtility.ParseQueryString(url);
        queryStrings.Set("observation_source", "[ObsValue]");
        Dictionary<string, string> queryStringsList = new Dictionary<string, string>();

        foreach (var key in queryStrings.AllKeys)
        {
            queryStringsList.Add(key, queryStrings[key]);
        }
        var newAddress = string.Empty;
        foreach (var item in queryStringsList)
        {
            newAddress = newAddress + item.Key + "=" + item.Value + "&";
        }
        Console.Write(newAddress);

I know its not proper but you can refine the code according to your needs. .

4 Comments

That's formatting a string, the OP is asking about replacing. Also, "try this" is not an answer, explain your solution.
@CodeCaster OP is asking about placeholders.
@Andrew no, that's their intended solution. The question starts with "I got a string", where &observation_source=3, and they don't know that it'll always be 3, and they'll want to replace that value. They're not building the query string from scratch, it's input they receive. See also the edit just made to the question.
I see in your edit you wrote "observation_source=[ObsSource]". My question is how I get the value of the "observation_source" to become "[ObsSource] so I can do something like a ".replace()" on it later, like I'm actually doing where it says [date]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.