VBScript - Get text inside string & remove text from string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ismailc
    New Member
    • Sep 2006
    • 200

    #1

    VBScript - Get text inside string & remove text from string

    Good day, I need help.

    I have 2 problems,

    1. I want to return the text within a string between [ ]: "New Description [1234]" - I want the value "1234"

    2. I want to remove the "[1234]" from the string, the problem is that the string differs in legth so i can't just remove the last 6 char.
    "New Description [1234]"
    "New Description [1234999]"
    I want to return "New Description"


    Please help.
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    dear,

    You have to look for the places of the "[" and the "]".

    like=

    form with:
    textbox = Text1
    1e label = Label1
    2e label = Label2
    command = Command1

    use the code=

    =============== =============== =============== ====

    Private Sub Command1_Click( )
    With Text1
    Label1.Caption = Mid(.Text, InStr(.Text, "[") + 1, InStr(.Text, "]") - 1 - InStr(.Text, "["))
    Label2.Caption = Left(.Text, InStr(.Text, "[") - 1)
    End With
    End Sub

    =============== =============== =============== ====

    - 1e looking for the value in the "[" and the "]"=
    You needs the string in the middle of a string so:
    Mid(string,...)
    The value starts at the place of the "[" +1 so:
    InStr(.Text, "[") + 1
    The lenght of the value is= place of "]"-1 min the place of the "[" so:
    InStr(.Text, "]") - 1 - InStr(.Text, "[")

    - 2e text before the value=
    The left of the string until the place of the "[" -1 so:
    Left(.Text, InStr(.Text, "[") - 1)

    I hope this will help you.

    br,
    Attached Files

    Comment

    • ismailc
      New Member
      • Sep 2006
      • 200

      #3
      Thank You all - i got it going

      tmpSStart = InStr(tmpSite,"[")
      tmpSEnd = InStr(tmpSite,"]")
      tmpSEnd = tmpSEnd -1
      tmpSChar = tmpSEnd - tmpSStart
      tmpSStart = tmpSStart + 1
      tmpSVal = Mid(tmpSite,tmp SStart,tmpSChar )

      Comment

      Working...