Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract a substring

Status
Not open for further replies.

vietnam97

Programmer
Sep 18, 2000
48
Hi,

is there a function in VB to extract a sub string out of a string? For example, I want to extract "Hello" out of "Hello World" string.

Thanks,
Bao

 
This should give you some ideas,

Private Sub Form_Load()

varTargetString = "Hello World"
varLookFor = "Hello"
varResult = Mid$(varTargetString, InStr(varTargetString, varLookFor), Len(varLookFor))
MsgBox varResult

End Sub
 
Extract or Replace?

Dim lngInstr as Long
Dim strFind as string
strFind = "hello" ' test

lngInstr = Instr(1,strHe,strFind,vbTextCompare)
if lngInstr > 0 then
strResult = Mid$(strHe,lngInstr, Len(strFind))
End if

Compare Code (Text)
Generate Sort in VB or VBScript
 
Warning. This statements throws an excepting if string not found because Instr will return 0 as a strting position and that is an invalid argument.

varResult = Mid$(varTargetString, InStr(varTargetString, varLookFor), Len(varLookFor))
Compare Code (Text)
Generate Sort in VB or VBScript
 
Good point John, that's why I'm a tech user, not a programmer.


Maybe you could help out with my thread? thread 222-189294

craig

craig@perfectform.com
 
Thanks John and Craig for helping out.

Bao
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top