String manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Qm9iQWNoZ2lsbA==?=

    #1

    String manipulation

    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
  • Spam Catcher

    #2
    Re: String manipulation

    =?Utf-8?B?Qm9iQWNoZ2l sbA==?= <BobAchgill@dis cussions.micros oft.com>
    wrote in news:52DFB19B-86AE-482A-BD3C-F7D4EDEE8190@mi crosoft.com:
    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(Direct ory, "/")

    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.

    Comment

    • Tom Shelton

      #3
      Re: String manipulation


      "BobAchgill " <BobAchgill@dis cussions.micros oft.comwrote in message
      news:52DFB19B-86AE-482A-BD3C-F7D4EDEE8190@mi crosoft.com...
      >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").S ubstring(0,3)

      HTH,
      --
      Tom Shelton

      Comment

      • Rad [Visual C# MVP]

        #4
        Re: String manipulation

        On Sun, 21 Oct 2007 22:43:04 -0600, "Tom Shelton"
        <tom_shelton@co mcastYOUKNOWTHE DRILL.netwrote:
        >
        >"BobAchgill " <BobAchgill@dis cussions.micros oft.comwrote in message
        >news:52DFB19 B-86AE-482A-BD3C-F7D4EDEE8190@mi crosoft.com...
        >>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").S ubstring(0,3)
        >
        >HTH,
        Rather than use substring, I think it would be wiser to use
        Path.GetExtensi on. File extensions are not always 3 characters, for
        example .HTML, .xslx, .ppxt, .docx etc

        --

        Comment

        • Andrew Morton

          #5
          Re: String manipulation

          Rad [Visual C# MVP] wrote:
          On Sun, 21 Oct 2007 22:43:04 -0600, "Tom Shelton"
          >dim stuff as string =
          >System.IO.Path .GetFileName("C :/Directory/___same.txt").S ubstring(0,3)
          >>
          Rather than use substring, I think it would be wiser to use
          Path.GetExtensi on. 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


          Comment

          • Tom Shelton

            #6
            Re: String manipulation

            On Oct 22, 2:58 am, "Rad [Visual C# MVP]" <r...@nospam.co mwrote:
            On Sun, 21 Oct 2007 22:43:04 -0600, "Tom Shelton"
            >
            >
            >
            >
            >
            <tom_shel...@co mcastYOUKNOWTHE DRILL.netwrote:
            >
            "BobAchgill " <BobAchg...@dis cussions.micros oft.comwrote in message
            news:52DFB19B-86AE-482A-BD3C-F7D4EDEE8190@mi crosoft.com...
            >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").S ubstring(0,3)
            >
            HTH,
            >
            Rather than use substring, I think it would be wiser to use
            Path.GetExtensi on. File extensions are not always 3 characters, for
            example .HTML, .xslx, .ppxt, .docx etc
            >
            --http://bytes.thinkersr oom.com- Hide quoted text -
            >
            - Show quoted text -
            I would have, except that the op wanted the first 3 chars, not the
            extension.

            --
            Tom Shelton

            Comment

            • Al  G

              #7
              Re: String manipulation


              "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
              news:Xns99D0D7B 2DF8A4usenethon eypotrogers@127 .0.0.1...
              =?Utf-8?B?Qm9iQWNoZ2l sbA==?= <BobAchgill@dis cussions.micros oft.com>
              wrote in news:52DFB19B-86AE-482A-BD3C-F7D4EDEE8190@mi crosoft.com:
              >
              >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(Direct ory, "/")
              >
              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


              Comment

              • CMoya

                #8
                Re: String manipulation

                "Tom Shelton" <tom_shelton@co mcastYOUKNOWTHE DRILL.netwrote in message
                news:utMYbYGFIH A.748@TK2MSFTNG P04.phx.gbl...
                >
                "BobAchgill " <BobAchgill@dis cussions.micros oft.comwrote in message
                news:52DFB19B-86AE-482A-BD3C-F7D4EDEE8190@mi crosoft.com...
                >>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").S ubstring(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).

                --
                -C. Moya



                HTH,
                --
                Tom Shelton

                Comment

                Working...