0

I have a string which basically looks like this:

/giveaway/host/setup/ref=aga_h_su_dp?_encoding=UTF8&value_id=1484778065

The trick here is that the length of the string can vary and will change... However the part with "value_id=something" always stays same... So the problem that I ran in was that I can do something like this:

var myId = string.SubString(30,45);

to get the value after value_id= /*this one here*/

I'm thinking that this can be solved by regex or some other way, but I'm not too sure how to write such one. Can someone help me out?

7
  • 1
    split by '=' then get last index of your string array Commented Oct 14, 2017 at 16:17
  • @arslanaybars what if there are multiple '=' signs in string ? Commented Oct 14, 2017 at 16:18
  • isn't it always and of the string ? @User987 Commented Oct 14, 2017 at 16:19
  • 1
    Use the Uri class. Commented Oct 14, 2017 at 16:19
  • 1
    @User987 seems like it is a part of an url. So no need for string operations. var value = HttpUtility.ParseQueryString(HttpUtility.HtmlDecode(input))["value_id"]; Commented Oct 14, 2017 at 16:24

2 Answers 2

1

Could you try this. If your value_id always the last of your string you can use this.

var str = "/giveaway/host/setup/ref=aga_h_su_dp?_encoding=UTF8&value_id=1484778065";

var valueId = str.Split('=').LastOrDefault();

Result : 1484778065

Hope it's help to you

Sign up to request clarification or add additional context in comments.

Comments

0

You can split by 'value_id='

string[] tokens = str.Split(new[] { "value_id=" }, StringSplitOptions.None);

if(tokens.Length>1){
   //parse tokens[1] with the value
}

3 Comments

good approach. @User987 you can use this also.
@arslanaybars would this work if there are multiple = ?
It will work, but if value_id= is the last. If you have other data after the value you need to separate it, then split again with the separator.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.