-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.

NachoDawg
  • 1,533
  • 11
  • 21
  • It looks like the [String.Format Method](https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx) would be useful to you. And yes, C# does have regexes. – Andrew Morton Oct 26 '16 at 09:28
  • See the [duplicate](http://stackoverflow.com/questions/772253/replace-item-in-querystring) for how to parse and modify query strings. Also, read [ask] and share your research. – CodeCaster Oct 26 '16 at 09: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 – NachoDawg Oct 26 '16 at 09:36
  • Yes, there are regexps in C#. Please google for "c# regex". –  Oct 26 '16 at 14:48

2 Answers2

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]");
});
  • 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 Oct 26 '16 at 09:45
  • 1
    @NachoDawg In .NET, `\d` in a regex means a [Unicode digit](http://www.fileformat.info/info/unicode/category/Nd/list.htm). You could use `[0-9]` instead to restrict it to the characters "0" to "9", or use [`RegexOptions.ECMAScript`](https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions(v=vs.110).aspx) to make it behave as an ECMAScript regex. The `+` means "one-or-more consecutive occurrences". – Andrew Morton Oct 26 '16 at 18:14
-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. .

Amey Khadatkar
  • 414
  • 3
  • 16
  • That's formatting a string, the OP is asking about replacing. Also, "try this" is not an answer, explain your solution. – CodeCaster Oct 26 '16 at 09:28
  • @CodeCaster OP is asking about placeholders. – Andrew Morton Oct 26 '16 at 09:29
  • 2
    @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. – CodeCaster Oct 26 '16 at 09:31
  • 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] – NachoDawg Oct 26 '16 at 09:33