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# / November 2006

Tip: Looking for answers? Try searching our database.

How to trim a text string?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
garyusenet@myway.com - 24 Nov 2006 10:51 GMT
Hi i would like to trim a text string.

I have a text string called filename

I want to display just the file in my label, and not the path.

Can someone tell me how I get everything AFTER, the LAST, BACKSLASH in
the string please?

Thankyou
ktrvnbq02@sneakemail.com - 24 Nov 2006 11:01 GMT
> Hi i would like to trim a text string.
>
[quoted text clipped - 4 lines]
> Can someone tell me how I get everything AFTER, the LAST, BACKSLASH in
> the string please?

I would not recommend string manipulation in this circumstance. Check
out the System.IO.Path class, which is designed for parsing paths and
will be able to extract the information you require. IIRC,
Path.GetFileName will return the data you require.

regards,

Matt
Marc Gravell - 24 Nov 2006 11:03 GMT
In this case, try Path.GetFileName();

Marc
garyusenet@myway.com - 24 Nov 2006 11:12 GMT
Thankyou I am using now : -

string path = OpenFile();
           string file = Path.GetFileName(path);
           lblFileName.Text = file;

which works a treat.

Can you tell me though. Is the path.GetFileName(path) just manipulating
the string in a similar fashion to what i first asked, but just
automatically? Or is it doing something fundamentally more different?

Thankyou very much,

Gary.
ktrvnbq02@sneakemail.com - 24 Nov 2006 11:31 GMT
> Can you tell me though. Is the path.GetFileName(path) just manipulating
> the string in a similar fashion to what i first asked, but just
> automatically? Or is it doing something fundamentally more different?

Essentially, probably yes. But the great thing about the Path class is
that it's all abstracted away from you, so have less worries about
compatibility with other platforms or future OSes, and there's less
chance for you to introduce an off-by-one bug or even a security
vulnerability caused by getting your app to parse malformed paths if
all the Path parsing is done centrally by the framework.

regards,

Matt
Marc Gravell - 24 Nov 2006 11:32 GMT
It is using standard methods, but the rules for how paths are formed are
complex - for instance, do you consider "/" and "\" to be equivalent? Have
you properly considered spaces / special characters in the path, etc.

The File class acts as a wrapper to this functionality, and is worth using.
In the more general caes there are lots of ways of working with strings; you
might have considered:

string a = @"abc\def\ghi"; // example path-esque string
string b = a.Substring(a.LastIndexOf('\\') + 1);

Marc
Arne Vajhøj - 24 Nov 2006 23:28 GMT
>  string path = OpenFile();
>             string file = Path.GetFileName(path);
[quoted text clipped - 5 lines]
> the string in a similar fashion to what i first asked, but just
> automatically? Or is it doing something fundamentally more different?

Probably the same.

(you can verify by decompiling Path.GetFileName)

The advantages are:
- your code are simpler
- your code are easier to read
- your code are safer because MS tests the framework and
  it has a lot of users who will report any bugs
- your code are better prepared for the future since
  if MS ever change the naming rules then they will also
  update Path.GetFileName to work with the new format

Arne

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.