可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is it possible to replace a substring in a string without assigning a return value?
I have a string:
string test = "Hello [REPLACE] world";
and I want to replace the substring [REPLACE]
with something else:
test = test.replace("[REPLACE]", "test");
This works fine but how can I do it without assigning the return value to a variable? I want something like this:
test.replace("[REPLACE]", "test");
回答1:
As mentioned by dlev, you can't do this with string
as strings are immutable in .NET - once a string has been constructed, there's nothing you can do (excluding unsafe code or reflection) to change the contents. This makes strings generally easier to work with, as you don't need to worry about defensive copying, they're naturally thread-safe etc.
Its mutable cousin, however, is StringBuilder
- which has a Replace
method to perform an in-object replacement. For example:
string x = "Hello [first] [second] world";
StringBuilder builder = new StringBuilder(x);
builder.Replace("[first]", "1st");
builder.Replace("[second]", "2nd");
string y = builder.ToString(); // Value of y is "Hello 1st 2nd world"
回答2:
You can't, because string
is immutable. It was designed so that any "changes" to a string would actually result in the creation of a new string
object. As such, if you don't assign the return value (which is the "updated" string, actually copy of the original string with applied changes), you have effectively discarded the changes you wanted to make.
If you wanted to make in-place changes, you could in theory work directly with a char[]
(array of characters), but that is dangerous, and should be avoided.
Another option (as pointed out by Mr. Skeet below) is to use StringBuilder
and its Replace()
method. That being said, simple replacements like the one you've shown are quite fast, so you may not want to bother with a StringBuilder
unless you'll be doing so quite often.
回答3:
You can't. You have to assign the value as strings are immutable
http://msdn.microsoft.com/en-us/library/362314fe.aspx
回答4:
Strings in .NET are immutable, they cannot be edited in-line.
The closest you can get to in-line editing is to create a StringBuilder
from a string, in-line fiddle with its contents and then get it to spit a string back out again. But this will still produce a new string rather than altering the original. It is a useful technique, though, to avoid generating lots of intermediary strings when doing lots of string fiddling, e.g. in a loop.
回答5:
You can't, strings are immutable in .NET
回答6:
You can't, in C# strings are immutable. Something like this would violate that. You need to have the return type of string because the one you're working with cannot change.
回答7:
Here is the code to fetch string from html and pass it StringBuilder and set value from your variable.You cannot do string.replace.You have to use StringBuilder while manipulating.Here in html page I add [Name] which is replaced by Name from code behind.make sure [Name] is unique or u can give any unique name.
string Name = txtname.Text;
string contents = File.ReadAllText(Server.MapPath("~/Admin/invoice.html"));
StringBuilder builder = new StringBuilder(contents);
builder.Replace("[Name]", Name);
StringReader sr = new StringReader(builder.ToString());