C# 4.0 更改字符串值。C#

C# 4.0 更改字符串值。C#,c#-4.0,C# 4.0,如何更改的字符串值 http://host/index.php?p=page 到 不确定,因为你在这里不清楚,但这是你要求的 string value = @"http://host/index.php?p=page"; value = @"http://host/index.php?p="; 另外,如果您想“参数化”字符串 String.Format("http://host/index.php?p={0}", variableName); 或者,更严重的是,您可能想要: string

如何更改的字符串值

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


不确定,因为你在这里不清楚,但这是你要求的

string value = @"http://host/index.php?p=page";
value = @"http://host/index.php?p=";
另外,如果您想“参数化”字符串

String.Format("http://host/index.php?p={0}", variableName);
或者,更严重的是,您可能想要:

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

在.NET中,字符串是不可变的,这意味着您不能更改字符串

您可以做的是从原始字符串创建一个新的字符串值,例如复制除最后四个字符以外的所有字符串:

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

这就是我如何理解你的问题,将删除最后一个“=”之后的任何内容


如果要删除第一个“=”字符后的所有内容:

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

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

您可以更具体一点吗?是否要获取字符串值,直到结束符号
=
为止?
url = url.Substring(0, url.Length - 4);
string s = @"http://host/index.php?p=page";
s = s.Remove(s.LastIndexOf("=")+1);
string s = @"http://host/index.php?p=page"
s = s.Split('=')[0] + "=";
String oldString = "http://host/index.php?p=page";
String newString = oldString.Substring(0, oldString.IndexOf("?p=") + 3);