Groups | Blog | Home
all groups > vb.net > may 2004 >

vb.net : Finding Character in String



Bernie Yaeger
5/22/2004 7:43:01 PM
Hi Mr B,

Take a look at the instrrev function.

HTH,

Bernie Yaeger

[quoted text, click to view]

Mr. B
5/22/2004 11:36:54 PM
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,

hirf-spam-me-here NO[at]SPAM gmx.at
5/23/2004 1:44:45 AM
* Mr. B <User@NoWhere.com> scripsit:
[quoted text, click to view]

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

--
Herfried K. Wagner [MVP]
Mr. B
5/23/2004 4:49:57 AM
[quoted text, click to view]

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

Thanks muchly!

Jon Skeet [C# MVP]
5/23/2004 8:04:47 AM
[quoted text, click to view]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
Cor Ligthert
5/23/2004 10:44:05 AM
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

[quoted text, click to view]

hirf-spam-me-here NO[at]SPAM gmx.at
5/23/2004 1:27:04 PM
* "Cor Ligthert" <notfirstname@planet.nl> scripsit:
[quoted text, click to view]

Thank you for explaining :-))).

Just my 2 Euro cents...

--
Herfried K. Wagner [MVP]
Mr. B
5/23/2004 4:11:54 PM
[quoted text, click to view]

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,

hirf-spam-me-here NO[at]SPAM gmx.at
5/23/2004 6:15:35 PM
* Mr. B <User@NoWhere.com> scripsit:
[quoted text, click to view]

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

--
Herfried K. Wagner [MVP]
Joe Fallon
5/23/2004 9:53:52 PM
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




[quoted text, click to view]

Mr. B
5/24/2004 4:29:53 AM
[quoted text, click to view]

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,

Cor Ligthert
5/24/2004 8:28:28 AM
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

Cor Ligthert
5/24/2004 10:07:52 AM
I forgot to mention John. Q. sorry John,

Cor

hirf-spam-me-here NO[at]SPAM gmx.at
5/24/2004 2:40:56 PM
* Mr. B <User@NoWhere.com> scripsit:
[quoted text, click to view]

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

--
Herfried K. Wagner [MVP]
hirf-spam-me-here NO[at]SPAM gmx.at
5/24/2004 7:23:49 PM
* "Joe Fallon" <jfallon1@nospamtwcny.rr.com> scripsit:
[quoted text, click to view]

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

--
Herfried K. Wagner [MVP]
AddThis Social Bookmark Button