String manipulation

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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.
 
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,
 
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
 
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
 
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.
 
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
 
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).
 
Back
Top