C # Changing a string after creating it

Ok, I know this question is very simple, and I admit that I'm pretty new to C #. But the name does not describe the whole situation here, so listen to me.

I need to change the URL string that is created in the C # code behind, removing the substring ".aspx" from the end of the string. So basically I know that my URL included in this class will be something like “Blah.aspx”, and I want to get rid of the “.aspx” part of this line. I guess this is pretty easy to do by simply finding this substring and deleting it if it exists (or some similar strategy, it would be useful if someone had an elegant solution if they thought they did it before ) Here is the problem:

"Since strings are immutable, it is impossible (without using unsafe code) to change the value of a string object after it is created." This is from the official MSDN website. Therefore, I am now wondering if the lines are really immutable, then I just can not (should not) change the line after it is created. So, how can I make sure that what I plan to do is safe?

+4
source share
4 answers

Line continuity is not a problem for normal use - it simply means that member functions such as Replace, instead of modifying an existing string object, return a new one. In practical terms, this usually means that you must remember to copy the change back to the original, for example:

string x = "Blah.aspx"; x.Replace(".aspx", ""); // still "Blah.aspx" x = x.Replace(".aspx", ""); // now "Blah" 

The strangeness around strings is due to the fact that System.String inherits from System.Object , but because of its immutability it behaves like a value type, not an object. For example, if you pass a string to a function, there is no way to change it unless you pass it by reference:

 void Test(string y) { y = "bar"; } void Test(ref string z) { z = "baz"; } string x = "foo"; Test(x); // x is still "foo" Test(ref x); // x is now "baz" 
+5
source

You do not change the line, you change the variable. Instead of this variable related to a string, such as "foo.aspx" , change it to point to a new string that has the value "foo" .

By analogy, adding one to the number two does not change the number two. Two still remain the same as always, you changed the variable, referring to one number, to refer to another.

As for your specific case, EndsWith and Remove make it simple enough:

 if (url.EndsWith(".aspx")) url = url.Remove(url.Length - ".aspx".Length); 

Note that Remove takes one line, an integer, and gives us a completely new line, which we need to assign back to our variable. It does not change the line.

Also note that there is a URI class that can be used to parse URLs, and it will be able to handle all complex situations that may arise, including hashes, request parameters, etc. You should use this to analyze aspects of the URL you are interested in.

+6
source

A String in C # is immutable as you say. This means that this would create several String objects in memory:

 String s = "String of numbers 0"; s += "1"; s += "2"; 

So, although the s variable will return you the value of String of numbers 012 , internally it required the creation of three lines in memory.

In your particular case, the solution is pretty simple:

 String myPath = "C:\\folder1\\folder2\\myFile.aspx"; myPath = Path.Combine(Path.GetDirectoryName(myPath), Path.GetFileNameWithoutExtension(myPath)); 

Again, it looks like myPath has changed, but it really hasn't. An internal copy and assignment has occurred, and you continue to use the same variable.

Alternatively, if you must save the original variable, you can simply create a new variable:

 String myPath = "C:\\folder1\\folder2\\myFile.aspx"; String thePath = Path.Combine(Path.GetDirectoryName(myPath), Path.GetFileNameWithoutExtension(myPath)); 

In any case, you will get a variable that you can use.

Note that using Path methods ensures that you get the correct paths, not blind String replacements, which can have unintended side effects.

+2
source

String.Replace() will not modify the string. He will create a new one. So the following code:

 String myUrl = @"http://mypath.aspx"; String withoutExtension = myUrl.Replace(".aspx", ""); 

will create a completely new line, which is assigned no exposure.

+1
source

Source: https://habr.com/ru/post/1496659/


All Articles