Parse URL with Regex C# [duplicate] - c#

This question already has answers here:
Get URL parameters from a string in .NET
(17 answers)
Closed 7 years ago.
I am getting the current URL with:
string url = HttpContext.Current.Request.Url.AbsoluteUri;
//url = blahblahblah.aspx?NUMBER=8798494651&FULLNAME=Ronald
After this, I want to use url.Split() to obtain the value after both equals signs. This would normally be straight forward but as you can see the first value has an obvious end character of '&' while the other does not. Furthermore, the FULLNAME value can always be different.
I have seen other Regex parsing questions/answers on here but of course they are all for specific cases.
Thanks in advance.

Try this
var uri = new Uri("blahblahblah.aspx?NUMBER=8798494651&FULLNAME=Ronald");
var query = HttpUtility.ParseQueryString(uri.Query);
var var1 = query.Get("NUMBER");
var var2 = query.Get("FULLNAME");

Related

Error when trying to do string var = "\" [duplicate]

This question already has answers here:
New Line in Constant error when trying to insert escape character in string
(3 answers)
Closed 4 years ago.
I am trying to do a Path.Combine. One string is the path and the other is just a slash.
string ok = browser.SelectedPath;
string okie = "\";
string pathy = Path.Combine(ok, okie);
Settings.Default["Path"] = pathy;
for
string okie = "\";
I get two errors NewLine in constant. Anyone know how to fix this?
Thank you!
Get rid of okie and pathy, and assign Settings.Default["Path"] the browser.SelectedPath, eg
Settings.Default["Path"] = browser.SelectedPath;

C# Selecting part of string when there is a matching result in part of the string [duplicate]

This question already has answers here:
Get URL parameters from a string in .NET
(17 answers)
Closed 5 years ago.
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?
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
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
}

c# replace character by an empty string [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I'm getting a string from a Json :
var value = JsonObject["price"]; //value = "1,560";
i'm trying to replace the ',' with an empty string :
value.Replace(",",string.Empty);
but i'm still getting the value with "," that's so strange and i'm stuck at it
thanks in advance
value = value.Replace( ", ", string.Empty);
strings in .net are immutable.
Per the documentation for String.Replace:
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It gives you a new string; it doesn't modify the existing one. So you need to assign the result to a variable:
value = value.Replace(",", string.Empty);

Invalid string in C# [duplicate]

This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 7 years ago.
How come this string is valid to open with VLC via a Process:
string fileToPlay = #"C:\Videos\Movies\Movie title.avi";
But this one isn't:
string fileToPlay = #myMovie;
Where the value of the variable myMovie is
"C:\Videos\Movies\Movie title.avi"
Process.Start(vlcPath, fileToPlay );
The problem is that you can only use the # character when placed against string literals like this:
string path = #"c:\temp";
It can be used when placed against a string variable, as you have done, but it has a different meaning. In that case, it is used when you choose an identifier which matches a C# keyword, like this:
string #class = "hello";
You can read more about it here: https://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx

How to get 4 digits from the middle a string in C# [duplicate]

This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 8 years ago.
I have a string variable like test10015, i want to get just the 4 digits 1001,
what is the best way to do it?
i"m working in asp.net c#
With Linq:
var expected = str.Skip(4).Take(4);
Without Linq:
var expected = str.Substring(4,4);
Select the first four digits in your string:
string str = "test10015";
string strNum = new string(str.Where(c => char.IsDigit(c)).Take(4).ToArray());
You can use String.Substring Method (Int32, Int32). You can subtract 5 from from the length to start from your required index. Make sure the format of string remains the same.
string res = str.Substring(str.Length-5, 4);
string input = "test10015";
string result = input.Substring(4, 4);

Categories