Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

String not removed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nadav - 30 Aug 2006 16:10 GMT
hi,
I have a string, I use the Remove method and the string stays the same.

ublic static bool CreatePrepareFile(string path)

{

Console.WriteLine(path);

int ind = path.LastIndexOf('\\');

Console.WriteLine("index is : {0}", ind);

path.Remove(ind);

This code is to take out the path out of a full filename. The index is
correct, I checked, but the string isn't removed as should!

why ?
Thanks.
Chris Dunaway - 30 Aug 2006 16:17 GMT
> hi,
> I have a string, I use the Remove method and the string stays the same.
[quoted text clipped - 13 lines]
> This code is to take out the path out of a full filename. The index is
> correct, I checked, but the string isn't removed as should!

Because the Remove method does not alter the string.  Instead if
returns a new string without the part to be removed.  To use the Remove
method properly, you would need code like this:

string s1 = "ABCDEF";
s1 = s1.Remove(....);

If you need to get a filename without the path, then just use the
Path.GetFileName() method from the System.IO namespace.

Chris
Andreas Mueller - 30 Aug 2006 16:17 GMT
> hi,
> I have a string, I use the Remove method and the string stays the same.
[quoted text clipped - 16 lines]
> why ?
> Thanks.

Strings are immutable, so all methods return a new string with the
desired modification. So if you write:

path = path.Remove(ind);

it should have the desired effect.

HTH,
Andy
Signature

You can email me directly by removing the NOSPAm below
xmenNOSPAm40@gmxNOSPAm.netNOSPAm


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.