Using Mid Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • halo combat22
    New Member
    • Oct 2007
    • 24

    Using Mid Function

    How do i use the Mid Function to grab the first and last letter of a word.
    User inputs word.

    ---Vb6---
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    First letter:
    Mid(stringName, 1,1)
    Last letter:
    Mid(stringName, stringName.Leng th, 1)

    explanation:
    - stringName is obvious what that stands for.
    - the second parameter is the position of the first character of the substring you want to create in the original string.
    - the third parameter is the length of the substring you want to create.
    Last edited by Killer42; Nov 13 '07, 02:39 AM. Reason: You need to practice spelling "lenght" :)

    Comment

    • jamesd0142
      Contributor
      • Sep 2007
      • 471

      #3
      Here's an example to make it easier...

      [code=vb]
      Dim sInput As String
      Dim sOutput As String
      sInput = textbox1.text

      ' First Character:
      sOutput = Mid(sInput, 1, 1)

      ' Last Character:
      sOutput = Mid(sInput, Len(sInput), 1)
      [/code]
      Last edited by Killer42; Nov 13 '07, 02:43 AM. Reason: Corrected "post-VB6" syntax on line 1.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Hm... does it have to be the Mid() function? Here's a slightly simpler version....

        [code=vb]
        Dim sInput As String
        Dim sOutput as string
        sInput = Textbox1.Text

        ' First Character:
        sOutput = Left(sInput, 1)

        ' Last Character:
        sOutput = Right(sInput, 1)[/code]

        Comment

        • Mohan Krishna
          New Member
          • Oct 2007
          • 115

          #5
          Hi everyone

          Please add
          sOutput = sOutput & Right(sInput, 1)
          at the last for the above code

          Comment

          • jamesd0142
            Contributor
            • Sep 2007
            • 471

            #6
            Originally posted by Mohan Krishna
            Hi everyone

            Please add
            sOutput = sOutput & Right(sInput, 1)
            at the last for the above code


            WHAT??????????? ??????????????? ???

            Comment

            • YarrOfDoom
              Recognized Expert Top Contributor
              • Aug 2007
              • 1243

              #7
              Originally posted by Killer42
              Hm... does it have to be the Mid() function? Here's a slightly simpler version....

              [code=vb]
              Dim sInput As String
              Dim sOutput as string
              sInput = Textbox1.Text

              ' First Character:
              sOutput = Left(sInput, 1)

              ' Last Character:
              sOutput = Right(sInput, 1)[/code]
              Right, I forgot. Probably because left() and right() aren't used in visual studio 2005 express

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by jamesd0142
                WHAT??????????? ??????????????? ???
                Mohan was pointing out that in demonstrating the two operations (first character, last character) we were both overwriting the value in sOutput. The result is that you would only actually get the last character.

                I was aware of that, but didn't worry about it. We weren't trying to provide finished code, just demonstrating some concepts.

                Comment

                • jamesd0142
                  Contributor
                  • Sep 2007
                  • 471

                  #9
                  Originally posted by Killer42
                  Mohan was pointing out that in demonstrating the two operations (first character, last character) we were both overwriting the value in sOutput. The result is that you would only actually get the last character.

                  I was aware of that, but didn't worry about it. We weren't trying to provide finished code, just demonstrating some concepts.
                  ahh thanks for the explanation.

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by jamesd0142
                    ahh thanks for the explanation.
                    No problem. :)

                    Explanations are what we do.

                    Comment

                    Working...