473,386 Members | 2,042 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,386 software developers and data experts.

Finding Character in String

I want to return the name of the drawing file (DWG) from the end of a string.
The string will be of varying lengths... as well as the drawing file name
itself.

I could do it the long way by getting the length of the string, and
subtracting one character at a time until I hit the '\' character... then
I'll know how much to subtract to get my file name.

An example of the string is:
"P:\Projects\2004\10-02-230 (ProjectName\600 Drawings\E101.dwg"

But is there an easier (and faster) way to search for the last '\' character
in my string? (keep in mind that the number of '\' in each string will also
vary).

Thanks in advance!

Regards,

Bruce
Nov 20 '05 #1
14 2143
Hi Mr B,

Take a look at the instrrev function.

HTH,

Bernie Yaeger

"Mr. B" <Us**@NoWhere.com> wrote in message
news:7o********************************@4ax.com...
I want to return the name of the drawing file (DWG) from the end of a string. The string will be of varying lengths... as well as the drawing file name
itself.

I could do it the long way by getting the length of the string, and
subtracting one character at a time until I hit the '\' character... then
I'll know how much to subtract to get my file name.

An example of the string is:
"P:\Projects\2004\10-02-230 (ProjectName\600 Drawings\E101.dwg"

But is there an easier (and faster) way to search for the last '\' character in my string? (keep in mind that the number of '\' in each string will also vary).

Thanks in advance!

Regards,

Bruce

Nov 20 '05 #2
* Mr. B <Us**@NoWhere.com> scripsit:
I want to return the name of the drawing file (DWG) from the end of a string.
The string will be of varying lengths... as well as the drawing file name
itself.

I could do it the long way by getting the length of the string, and
subtracting one character at a time until I hit the '\' character... then
I'll know how much to subtract to get my file name.


Use 'System.IO.Path.GetFileName' instead.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
With Deft Fingers, Mr. B <Us**@NoWhere.com> wrote:
I want to return the name of the drawing file (DWG) from the end of a string.
The string will be of varying lengths... as well as the drawing file name
itself.


Yeow! Thanks ALL... so many options to choose from... I'll try them all and
see where I get.

Thanks muchly!

bruce
Nov 20 '05 #4
Mr. B <Us**@NoWhere.com> wrote:
With Deft Fingers, Mr. B <Us**@NoWhere.com> wrote:
I want to return the name of the drawing file (DWG) from the end of a string.
The string will be of varying lengths... as well as the drawing file name
itself.


Yeow! Thanks ALL... so many options to choose from... I'll try them all and
see where I get.


While there are indeed lots of ways of approaching this, I'd be
surprised if Path.GetFileName or Path.GetFileNameWithoutExtension
didn't turn out to be the best one here, as they do *exactly* what it
sounds like you need.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 20 '05 #5
Hi Jon,

That was what Herfried had answered already in the VB.language group,
however you can not see that because Herfried is using a newsreader which
cannot crosspost when it is not in his range of standard newsgroups.

Therefore you was probably suprissed by the answer of mr. B.

Just to make it more clear for you.

Cor
While there are indeed lots of ways of approaching this, I'd be
surprised if Path.GetFileName or Path.GetFileNameWithoutExtension
didn't turn out to be the best one here, as they do *exactly* what it
sounds like you need.

Nov 20 '05 #6
* "Cor Ligthert" <no**********@planet.nl> scripsit:
That was what Herfried had answered already in the VB.language group,
however you can not see that because Herfried is using a newsreader which
cannot crosspost when it is not in his range of standard newsgroups.


Thank you for explaining :-))).

Just my 2 Euro cents...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7
With Deft Fingers, hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote:
I want to return the name of the drawing file (DWG) from the end of a string.
The string will be of varying lengths... as well as the drawing file name
itself.
Use 'System.IO.Path.GetFileName' instead.


Hmmm... in thinking about this... I'm not sure this will work. As I am trying
to extract the file name from a 'string' (knowing that it is after the last \
character).. not look for it on a drive! But I'll try it anyways.

Regards,

Bruce
Nov 20 '05 #8
* Mr. B <Us**@NoWhere.com> scripsit:
I want to return the name of the drawing file (DWG) from the end of a string.
The string will be of varying lengths... as well as the drawing file name
itself.

Use 'System.IO.Path.GetFileName' instead.


Hmmm... in thinking about this... I'm not sure this will work. As I am trying
to extract the file name from a 'string' (knowing that it is after the last \
character).. not look for it on a drive! But I'll try it anyways.


Mhm... How do your path strings look like?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #9
I agree that these are the best :Path.GetFileName or
Path.GetFileNameWithoutExtension .

One alternative for finding the position of the last character in a string
which is not a file name is:
strFoo.LastIndexOf("\")

Dim somestring As String = "this is a long string"
Dim i As Integer = somestring.LastIndexOf("s")

i =15

--
Joe Fallon


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mr. B <Us**@NoWhere.com> wrote:
With Deft Fingers, Mr. B <Us**@NoWhere.com> wrote:
I want to return the name of the drawing file (DWG) from the end of a string.The string will be of varying lengths... as well as the drawing file nameitself.


Yeow! Thanks ALL... so many options to choose from... I'll try them all and see where I get.


While there are indeed lots of ways of approaching this, I'd be
surprised if Path.GetFileName or Path.GetFileNameWithoutExtension
didn't turn out to be the best one here, as they do *exactly* what it
sounds like you need.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 20 '05 #10
With Deft Fingers, hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote:
Hmmm... in thinking about this... I'm not sure this will work. As I am trying
to extract the file name from a 'string' (knowing that it is after the last \
character).. not look for it on a drive! But I'll try it anyways.


Mhm... How do your path strings look like?


What I had initially shown as an example is:

An example of the string is:
"P:\Projects\2004\10-02-230 (ProjectName\600 Drawings\E101.dwg"

(: Pretty standard stuff... but I guess I should have explained that I get
this 'string' from a TXT type file and not the HD (sorry... silly me for not
explaining better) :(

Regards,

Bruce
Nov 20 '05 #11
Hi Joe,

That is what Bernie posted (although with the VB function equivalent from
it) and Herfried and Jon did give both independent from each other the
better alternative GetFilePath.

In this thread even a C# diehard as Jon did not come with the LastIndexOf. I
can assure you that all other posters active in this thread do know that
method very well. It was (by me, however I think also by the others)
express not given, so the OP would not become confused.

Do you think you help the OP with telling this worse method for this
problem.

Cor
Nov 20 '05 #12
I forgot to mention John. Q. sorry John,

Cor
Nov 20 '05 #13
* Mr. B <Us**@NoWhere.com> scripsit:
Hmmm... in thinking about this... I'm not sure this will work. As I am trying
to extract the file name from a 'string' (knowing that it is after the last \
character).. not look for it on a drive! But I'll try it anyways.


Mhm... How do your path strings look like?


What I had initially shown as an example is:

An example of the string is:
"P:\Projects\2004\10-02-230 (ProjectName\600 Drawings\E101.dwg"

(: Pretty standard stuff... but I guess I should have explained that I get
this 'string' from a TXT type file and not the HD (sorry... silly me for not
explaining better) :(


But then 'Path.GetFileName' will work, won't it?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #14
* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:
One alternative for finding the position of the last character in a string
which is not a file name is:
strFoo.LastIndexOf("\")


The character is "\" for Windows systems, but what about a path that
uses "/" to separate folders?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #15

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

Similar topics

5
by: lawrence | last post by:
"Garp" <garp7@no7.blueyonder.co.uk> wrote in message news:<_vpuc.1424$j_3.13346038@news-text.cableinet.net>... > "lawrence" <lkrubner@geocities.com> wrote in message >...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
2
by: B Moor | last post by:
I have a database with 100,000's records, each with a unique reference, eg A123BNK456 I would like to generate a search facility whereby we can choose an exact match or partial match, where the...
2
by: Mike P | last post by:
I want to find a particular character in a string, and then find the length of the string after this point in the string, so that I can do a rightpad on it if it is of a certain length (basically I...
4
by: David Warner | last post by:
Greetings! In looking into some C coding, I am looking for the C function that will search for multiple occurances of a same character in a string. For Instance: char str = "We the people...
9
by: Mr. B | last post by:
I want to return the name of the drawing file (DWG) from the end of a string. The string will be of varying lengths... as well as the drawing file name itself. I could do it the long way by...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
2
by: Lucas Kruijswijk | last post by:
Hello, I am making some site, where I use UTF-8 encoding. From PHP I send mail. But, if possible I want to send the mail in ISO-8859-1 or KOI8-R (because still some mailers have problem with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.