Edit and find querystring parameter value [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
First I want to say that I'm completely new to coding and although I've learned some basics a few years ago.
For now I'm trying to make a program that searches in an user inputted text. And replace some parts of that, then gives the new text back.
Like this:
stormcloud-146919.appspot.com/purchase/listing/?global=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICAgOSRCgw&coin=3&xpPlayer=10&sidekick1=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICAkMmPCgw&xpSidekick1=10&sidekick2=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICA0OqZCgw&xpSidekick2=10&k=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ciwLEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDA&l=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5chQLEgdMaXN0aW5nGICAgID00JwKDA&
With the bold parts being searched and replaced by my custom text or digits.

I hope you are looking to update some querystring parameter values.
You can use HttpUtility.ParseQueryString() for this.
Find the sample below.
var url = "stormcloud-146919.appspot.com/purchase/listing/?global=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICAgOSRCgw&coin=3&xpPlayer=10&sidekick1=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICAkMmPCgw&xpSidekick1=10&sidekick2=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ckULEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDAsSDEl0ZW1JbnN0YW5jZRiAgICA0OqZCgw&xpSidekick2=10&k=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5ciwLEgNVSUQiEDEyNTA0NjQ5MDgzMjUyNDIMCxIGUGxheWVyGICAgICAgIAKDA&l=ahNzfnN0b3JtY2xvdWQtMTQ2OTE5chQLEgdMaXN0aW5nGICAgID00JwKDA&";
Console.WriteLine(url);
var uriBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["coin"] = "4";
query["xpPlayer"] = "4";
query["xpSidekick1"] = "4";
query["xpSidekick2"] = "4";
uriBuilder.Query = query.ToString();
Console.WriteLine(uriBuilder.ToString());
See if that meets your requirements.
Remember to import System.Web namespace.
using System.Web;
More details on the code an be found here:
http://codeskaters.blogspot.ae/2016/12/c-update-querystring-values-in-url.html

You can use
string [] split = input.Split(new Char [] {'&'});
And the for each split .Split(new Char [] {'='});
Replace needed values and concat all parts to new string.

Related

Removing the commas from string in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this string:
AppleBanana
With C# code, I was managed to bring like this:
Apple,Banana,
But I don't want the last comma in it.
So for that I used:
string obj = myVar.Trim().Trim(','); // this is just to remove the whitespace and trailing comma
But nothing happens. My string is still the same i.e. it shows Apple,Banana,.
Then I tried this:
if (myVar.EndsWith(", "))
myVar= myVar.Remove(myVar.Length - 1); // but this removes all the commas from the string.
Where am I missing?
string s = "Apple,Banana,";
s = s.Remove(s.Length - 1);
It's clear that you're working with comma-delimited data, so treat it as such. Split it into an array. While you're splitting it, you can remove empty entries, which will have the effect of ignoring the final comma.
var list = myVar.Split(',', StringSplitOptions.IgnoreEmptyEntries);
If you need it as a comma-separated string again, join it:
var myVar = string.Join(",", list);
Remove the last character.
string obj = myVar.Remove(myVar.Length - 1);
Update
#JohnWu solution is better, more coherent, more robust.

split user:pass to user and pass in c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So basically I have this program where the user uploads a list of user:pass in that format and I need user to be in it's own string and pass to be in it's own string.
Example of what i tried
string Account = listBox1.SelectedIndex.ToString();
char[] delimiterChars = { ':' };
Account.Split(delimiterChars);
I need to get the front part of user into string user and back part into string pass
Use string.split:
var combined = "user:pass";
var split = combined.Split(new[]{":"});
var user = split[0];
var password = split[1];

String contains a lot of copies, how to get rid of them? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I got something like this:
string s="Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;"
and now I want to get rid of the copies in the string...so that in the end s should be like this:
s="Solid;Gass;Liquid;"
Try this:
var parts = s.Split(';');
var distinctParts = parts.Distinct();
var newString = string.Join(";", distinctParts);
Where:
Split will give you an array with all the words of your string, taking the specified character as the word separator (; in this case).
Distinct will give your a collection with the unique words of your array.
Finally, Join composes a new string with the unique words, using the specified string (;in this case) as the separator.
You can split the string, then find the distinct instances and join them back in one line:
string s = "Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;";
s = string.Join(";", s.Split(';').Distinct());

how to remove particumar letter combination from a string in c#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a string like below
he/a0h/a0dv/a0jks
I would like to be string become as below.
hehdvjks
Need to remove the "/a0" from the string.
The String.Replace method is what you're looking for, just do a;
String myString = "he/a0h/a0dv/a0jks"
myString = myString.Replace("/a0", "")
It'll return a modified 'myString' with all occurrences of the old value ("/a0") replaced with a new value (blank in this case).
The MSDN reference for String.Replace can be found at:
http://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx
var result = "he/a0h/a0dv/a0jks".Replace("/a0", string.Empty);

Simple solution for #"\b \b" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying read one string from TextBox and use in my script.
Suppose this TextBox name is: txt3.
I want read this value and use in below lines:
string s = Regex.Replace(str,
#"\btxt3.Text\b",
txt4.Text,
RegexOptions.IgnoreCase);
How I can write this #"\btxt3.Text\b" ?
I want write that as :
string str==#"\btxt3.Text\b";
You want something like:
String.Format(#"\b{0}\b", txt3.Text)
Try this
string s = Regex.Replace(str, string.Format(#"\b{0}\b",txt3.Text), txt4.Text, RegexOptions.IgnoreCase);
If you want to combine value of txt3 with other strings, one way to do it is write
"\\b" + txt3.Text + "\\b"
instead of
#"\btxt3.Text\b"

Categories