473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to trim a text string?

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

Nov 24 '06 #1
6 11586
ga********@myway.com wrote:
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

Nov 24 '06 #2
In this case, try Path.GetFileName();

Marc
Nov 24 '06 #3
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.

Nov 24 '06 #4
ga********@myway.com wrote:
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

Nov 24 '06 #5
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
Nov 24 '06 #6
ga********@myway.com wrote:
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
Nov 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mikael Hellström | last post by:
Hi all, i need to use the trim function trim("str","?") My question are. How do i remove the "return sign" from a string?? Regards Mikael
22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
5
by: Alex Vassiliev | last post by:
Hi all. Just wanted to share two handy RegEx expressions to strips leading and trailing white-space from a string, and to replace all repeated spaces, newlines and tabs with a single space. *...
12
by: Robert Mark Bram | last post by:
Hi All, I am using the following trim function: function trim (str) { return str.replace(/^\s*/g, '').replace(/\s*$/g, ''); } The problem is that this doesn't trim instances of the...
3
by: Andy B | last post by:
I've tried using Trim or RTrim to strip trailing space characters from my data. When I check on the transformed data space characters are still there. We have an address table containing two...
13
by: Jonathan Wood | last post by:
According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear to...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
3
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
8
by: Kevin Smith | last post by:
Hi, According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear...
2
by: Peter Duniho | last post by:
On Wed, 08 Oct 2008 11:26:01 -0700, Kevin Smith <no@spam.comwrote: No, it wouldn't. Nor would it modify the String instance that s refers to. Strings are immutable. They are _never_...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.