473,799 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11621
ga********@mywa y.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.GetFileNam e will return the data you require.
regards,

Matt

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

Marc
Nov 24 '06 #3
Thankyou I am using now : -

string path = OpenFile();
string file = Path.GetFileNam e(path);
lblFileName.Tex t = file;

which works a treat.

Can you tell me though. Is the path.GetFileNam e(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********@mywa y.com wrote:
Can you tell me though. Is the path.GetFileNam e(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\gh i"; // example path-esque string
string b = a.Substring(a.L astIndexOf('\\' ) + 1);

Marc
Nov 24 '06 #6
ga********@mywa y.com wrote:
string path = OpenFile();
string file = Path.GetFileNam e(path);
lblFileName.Tex t = file;

which works a treat.

Can you tell me though. Is the path.GetFileNam e(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.GetFileNam e)

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.GetFileNam e 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
1792
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
12767
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' or even why there could be a problem. here is the code ////////
5
33336
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. * JavaScript example: String.prototype.trim = function() { // Strip leading and trailing white-space
12
47962
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 " " char - the non breaking space. Can this be represented in a grep statement at
3
10702
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 fields: BuildName and RoadName. Both have the following properties: size 50, not indexed, not required, allowed zero length. Some records have BuildName, RoadName as null, some have content. No content is 50 chr long. When i run a Len(BuildName)...
13
2650
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 modify s. s.Trim('\r'); While the follow code DOES modify s. s = s.Trim(\r');
22
9756
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 thought I'd change it to the VB ..NET method "String.Trim" but that throws an object exception. Which brings the question: is it compliant to use Trim(String), or is it more within etiquette to use If Not String Is Nothing Then String.Trim?
3
3505
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 read in msdn : and notice that trim only Removes all occurrences of white space characters from the beginning and end of this instance. So what for the middle ? .NET Framework Class Library
8
2844
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 to modify s. s.Trim('\r'); While the follow code DOES modify s.
2
698
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_ modified, by any method. Yes, the variable s is modified, so that it refers to a new instance of the string with the whitespace removed. The original instance is not modified.
0
9689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.