How to trim a text string?

  • Thread starter Thread starter garyusenet
  • Start date Start date
G

garyusenet

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
 
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?

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
 
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.
 
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
 
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
 
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top