Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Remove doesn't work [duplicate]

Tags:

c#

I have such code and can't understand where is the mistake, despite the fact, that this code pretty easy. So q is a full path, and I need to get required path to Gen_ParamFile

string q = @"C:\ProgramData\RadiolocationQ\script-Data=12^6-12^33.xml";
string _directoryName1 = @"C:\ProgramData\RadiolocationQ";
int Length = _directoryName1.Length + "ascript".Length; 

 string Gen_ParamFile = q;
 Gen_ParamFile.Remove(0, Length); // this line don't  do anything

var Gen_Parfile = Path.Combine(_directoryName1, "GeneralParam-Data" + Gen_ParamFile);

I used function like said here http://msdn.microsoft.com/ru-ru/library/9ad138yc(v=vs.110).aspx

like image 203
Rocketq Avatar asked Feb 08 '26 03:02

Rocketq


2 Answers

It does, it just doesn't affect the actual string, it creates a new one as a result. Use:

Gen_ParamFile = Gen_ParamFile.Remove(0, Length);
like image 100
S_F Avatar answered Feb 12 '26 05:02

S_F


Because String.Remove method returns a new string. It doesn't change original one.

Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

Remember, strings are immutable types. You can't change them. Even if you think you change them, you actually create new strings object.

You can assign itself like;

Gen_ParamFile = Gen_ParamFile.Remove(0, Length);

As an alternative, you can use String.SubString method like;

Gen_ParamFile = Gen_ParamFile.SubString(Length);
like image 40
Soner Gönül Avatar answered Feb 12 '26 05:02

Soner Gönül



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!