Finding Character in String

  • Thread starter Thread starter Mr. B
  • Start date Start date
M

Mr. B

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
 
* Mr. B said:
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.
 
With Deft Fingers said:
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
 
Mr. B said:
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.
 
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
 
* "Cor Ligthert said:
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...
 
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
 
* Mr. B said:
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?
 
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
 
With said:
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
 
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
 
* Mr. B said:
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?
 
* "Joe Fallon said:
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?
 
Back
Top