Machie
Machie

Reputation: 9

Change string value. C#

How do i change a string value of

http://host/index.php?p=page 

to

http://host/index.php?p=

Upvotes: 0

Views: 4181

Answers (8)

Jack Marchetti
Jack Marchetti

Reputation: 15754

Also, if you're lookin to "parameterize" the string.

String.Format("http://host/index.php?p={0}", variableName);

Upvotes: 0

Romany Saad
Romany Saad

Reputation: 96

Here's one more way:

String oldString = "http://host/index.php?p=page";
String newString = oldString.Substring(0, oldString.IndexOf("?p=") + 3);

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564671

If you want to strip off everything after the first "=" character:

string s = @"http://host/index.php?p=page"
s = s.Split('=')[0] + "=";

Upvotes: 1

Oskar Kjellin
Oskar Kjellin

Reputation: 21900

This is how I understood your question, will remove anything after the last "="

string s = @"http://host/index.php?p=page";
s = s.Remove(s.LastIndexOf("=")+1);

Upvotes: 1

Guffa
Guffa

Reputation: 700592

That is not possible.

In .NET strings are immutable, which means that you can't change a string.

What you can do is to create a new string value from the original, for example by copying all of the string except the last four characters:

url = url.Substring(0, url.Length - 4);

Upvotes: 5

Jacob G
Jacob G

Reputation: 3665

string s = @"http://host/index.php?p=page";
s = s.Replace("page", "");

Or, more seriously, you probably want:

string s = @"http://host/index.php?p=page";
s = s.Substring(0, s.LastIndexOf('=') + 1);

Upvotes: 1

Gabe
Gabe

Reputation: 50513

Not sure, since you aren't being to clear here, but this does what you ask.

string value = @"http://host/index.php?p=page";
value = @"http://host/index.php?p=";

Upvotes: 2

Ian Jacobs
Ian Jacobs

Reputation: 5501

string s=@"http://host/index.php?p=page";

s=@"http://host/index.php?p=";

Upvotes: 1

Related Questions