?
Solved

Search for characters in a string (VBScript)

Posted on 2008-10-21
6
Medium Priority
?
2,600 Views
Last Modified: 2012-05-05
I am trying to search for a word within a string. For example the string "This is a test string". I want to find the word "test" within this string and then remove all the text before "test". The result of the string should be "test string". How would I do this using vbscript?
0
Comment
Question by:illfusion82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 2000 total points
ID: 22769987
You can get the position of the substring using the InStr function, then get all characters from that point on by using Mid.
http://www.w3schools.com/vbscript/func_instr.asp
http://www.w3schools.com/vbscript/func_mid.asp


strOld = "This is a test string"
strToFind = "test"
 
intPos = InStr(strOld, strToFind)
strNew = Mid(strBase, intPos)

Open in new window

0
 
LVL 3

Expert Comment

by:DrunkenELF
ID: 22770000
Dear IllFusion,
Try the attached code.
Cheers!
D-ELF

MyVar = "This is a test string"
Right(MyVar ,InStr (MyVar,test))

Open in new window

0
 
LVL 38

Expert Comment

by:Shift-3
ID: 22770007
Correction to mine.


strOld = "This is a test string"
strToFind = "test"
 
intPos = InStr(strOld, strToFind)
strNew = Mid(strOld, intPos)
 
WScript.Echo strNew

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:DrunkenELF
ID: 22770032
Correction to mine too
MyVar = "This is a test string"
Result = Right(MyVar ,InStr (MyVar,test))
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22771162
Hi, as an addition to Shift-3's code, this will tell you whether the string was found or not as well, and also has the option of a case sensitive find.

Regards,

Rob.
strOriginalString = "This is a test string"
strStringToFind = "test"
 
boolCaseSensitive = True
 
If boolCaseSensitive = True Then
	intPos = InStr(strOriginalString, strStringToFind)
Else
	intPos = InStr(LCase(strOriginalString), LCase(strStringToFind))
End If
 
If intPos > 0 Then
	strNewString = Mid(strOriginalString, intPos)
	WScript.Echo "The new string is" & VbCrLf & strNewString
Else
	WScript.Echo strStringToFind & VbCrLf & "was not found in" & VbCrLf & strOriginalString & _
		VbCrLf & VbCrLf & "Case Sensitive: " & boolCaseSensitive
End If

Open in new window

0
 
LVL 1

Author Closing Comment

by:illfusion82
ID: 31508435
Thanks shift-3 and everyone that posted a response. I tried shift-3's resolution and it worked perfectly!
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question