String manipulation

G

Guest

I have an array of directory strings that I need to cut out just the three
characters located here in this spot "___" in this set of strings. The stuff
before "___" can vary in length but the stuff after the "___" is always the
same length. How is the best way to cut off the stuff before and after and
just leave the "___"??

eg.
C://Directory/___same.txt
C://Directory/___same.txt
C://Directory/___same.txt
C://Directory/___same.txt

THanks!

Bob
 
G

Guest

I have an array of directory strings that I need to cut out just the
three characters located here in this spot "___" in this set of
strings. The stuff before "___" can vary in length but the stuff
after the "___" is always the same length. How is the best way to cut
off the stuff before and after and just leave the "___"??

eg.
C://Directory/___same.txt
C://Directory/___same.txt
C://Directory/___same.txt
C://Directory/___same.txt

Take a look at the Left/Right functions and InStrRev.

You know same.txt = 7 chars.

You know the total length.

You can get the position of the last slash using InStrRev.

So:

Length = 25
Ending = 7
Index of Last Slash = InStrRev(Directory, "/")

Fixed String = Left(Directory, 0, Length - Index of Last Slash) + Right
(Directory, 7)

Or something along that line.

You can also take a look at Regular Expressions too.
 
T

Tom Shelton

BobAchgill said:
I have an array of directory strings that I need to cut out just the three
characters located here in this spot "___" in this set of strings. The
stuff
before "___" can vary in length but the stuff after the "___" is always
the
same length. How is the best way to cut off the stuff before and after
and
just leave the "___"??

eg.
C://Directory/___same.txt
C://Directory/___same.txt
C://Directory/___same.txt
C://Directory/___same.txt

THanks!

Bob

Sweet... So, you have a path, and you need to get stuff from the file
name....

dim stuff as string =
System.IO.Path.GetFileName("C:/Directory/___same.txt").Substring(0,3)

HTH,
 
R

Rad [Visual C# MVP]

Sweet... So, you have a path, and you need to get stuff from the file
name....

dim stuff as string =
System.IO.Path.GetFileName("C:/Directory/___same.txt").Substring(0,3)

HTH,

Rather than use substring, I think it would be wiser to use
Path.GetExtension. File extensions are not always 3 characters, for
example .HTML, .xslx, .ppxt, .docx etc
 
A

Andrew Morton

Rad said:
Rather than use substring, I think it would be wiser to use
Path.GetExtension. File extensions are not always 3 characters, for
example .HTML, .xslx, .ppxt, .docx etc

I think the OP was after the first three characters of the filename, not the
extension.

(And presumably he also meant to use backslashes as path separators.)

Andrew
 
T

Tom Shelton

Rather than use substring, I think it would be wiser to use
Path.GetExtension. File extensions are not always 3 characters, for
example .HTML, .xslx, .ppxt, .docx etc

--http://bytes.thinkersroom.com- Hide quoted text -

- Show quoted text -

I would have, except that the op wanted the first 3 chars, not the
extension.
 
A

Al G

Spam Catcher said:
Take a look at the Left/Right functions and InStrRev.

You know same.txt = 7 chars.

You know the total length.

You can get the position of the last slash using InStrRev.

So:

Length = 25
Ending = 7
Index of Last Slash = InStrRev(Directory, "/")

Fixed String = Left(Directory, 0, Length - Index of Last Slash) + Right
(Directory, 7)

Or something along that line.

You can also take a look at Regular Expressions too.


Thanks, I didn't know there was an InStrRev!

Al G
 
C

CMoya

Tom Shelton said:
Sweet... So, you have a path, and you need to get stuff from the file
name....

dim stuff as string =
System.IO.Path.GetFileName("C:/Directory/___same.txt").Substring(0,3)

What happens if the filename happens to be less than 3 characters?
Won't this throw an exception? I'd use VB's helpful, tried and true helper
function Left/Mid/Right in this case (and in most cases).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top