Solved

Search for characters in a string (VBScript)

Posted on 2008-10-21
6
2,590 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
6 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now