Connecting Tech Pros Worldwide Forums | Help | Site Map

VBScript - Get text inside string & remove text from string

Familiar Sight
 
Join Date: Sep 2006
Posts: 142
#1: 3 Weeks Ago
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.

Newbie
 
Join Date: Oct 2009
Posts: 18
#2: 2 Weeks Ago

re: VBScript - Get text inside string & remove text from string


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
File Type: zip cut text parts.zip (1.3 KB, 1 views)
Familiar Sight
 
Join Date: Sep 2006
Posts: 142
#3: 2 Weeks Ago

re: VBScript - Get text inside string & remove text from string


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,tmpSStart,tmpSChar)
Reply


Similar Visual Basic 4 / 5 / 6 bytes