Oliver Kucharzewski
Oliver Kucharzewski

Reputation: 2645

How can i use defined strings within my string

Uhh Sorry its really hard to explain but.

This is already a string:

"login_username=Username&login_password=Password&login_submit=Submit"

How can i replace 'Username' with a pre-defined string called User? Would it require Quotes?

Upvotes: 0

Views: 101

Answers (4)

Tim Schmelter
Tim Schmelter

Reputation: 460108

Tor replace Strings with Strings you use String.Replace.

var str  = "login_username=Username&login_password=Password&login_submit=Submit";
var str2 = str.Replace("Username", "User");

or if your User is a string variable:

str2 = str.Replace("Username", User);

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500465

Do you mean you have a string variable called user? If so, something like:

string parameters = "login_username=" + user + 
                    "&login_password=Password&login_submit=Submit";

Or, better:

string parameters = string.Format(
      "login_username={0}&loginPassword={1}&login_submit=Submit",
      user, password);

Or you could use (if you can't change the code and have to take the original string with the hard-coded user name):

string updated = fixedParameters.Replace("login_username=Username",
                                         "login_username=" + user);

Upvotes: 5

Chuck Norris
Chuck Norris

Reputation: 15190

string str="login_username=Username&login_password=Password&login_submit=Submit";
str User="Hamlet";
str.Replace("Username",User);

Or if you don't have str pre-defined you can write like this

string User= "Hamlet";
string str= "login_username=" + userName + "&login_password=Password&login_submit=Submit"

Upvotes: 1

Paolo Tedesco
Paolo Tedesco

Reputation: 57202

You are asking how to concatenate strings, right?

string userName = "SomeUserName";
string value = "login_username=" + userName + "&login_password=Password&login_submit=Submit"

Upvotes: 0

Related Questions