The VBScript Network and Systems Administrator's Cafe:

String manipulation

Jan 30 2009   1:30AM GMT

Easy String Searches with the VBScript Instr function



Posted by: Jerry Lees
InStr function, String manipulation, strings, string, VBScript, Scripting

I recently had a situation where I needed to find string inside a string to parse the larger string into usable and separate chunks of data. In writing this script, I realized I’ve not shared this gem of a function with you previously… and it really makes matters easier when you are manipulating strings.

That function is the Instr function. When configured with the proper parameters, by default, it will return the FIRST location in a string where a sub-string occurs. (Hint: this is where a mid() function would come in handy after you have that starting location). If the sub-string is not found, the function returns 0. The syntax is as follows:

Instr(StartPostion, StringToSearch,StringtoFind, Compare)

 

 The script I wrote really is a blog entry of it’s own, and will be, but I wanted to first introduce you to this great function and let you have a chance to play with it and see some code in action. Below is an example script that takes a string and searched for specific sub strings, then returns the location in the string where the sub string occurs.

 

Option explicit
Dim StrSource

StrSource = “Now I can find a word In a long string of words in a sentance.”

WScript.Echo(InStr(1,StrSource,”In”))
WScript.Echo(InStr(1,StrSource,”in”))
WScript.Echo(InStr(1,StrSource,” in “))
WScript.Echo(InStr(1,StrSource,”now”))

Below is the output from the script. Notice, the following:

  • The case of in and In in the sub-string being searched for and the positions it returns?
  • Notice the substring “in ” returns something entirely different?
  • Notice the substring now returns 0?

 

23
12
48
0

 

Enjoy!

Dec 29 2008   3:21PM GMT

Using special characters in VBScript strings



Posted by: Jerry Lees
strings, String manipulation, string, working with variables, Special Characters, ASCII, VBScipt

I just realized I’ve been using Visual Basic String constants in my scripts with little to no explanation. We’ll rectify that situation right now! ;-)

When dealing with strings in VBScript, you occasionally need to add formatting characters to a string, so that when it is displayed on the screen or in a file it looks correct. If you know your ASCII chart you can certainly preform this with a “& chr(X) &”.However, if you don’t know what ASCII value a tab is (it’s 9) then you can simply use Visual Basic’s string constants in your scripts and have the added benefit if making it easier to read!

The constants and their meanings are listed below:

VBTab A Tab character [Chr(9)]
VBCr A carriage return [Chr(13)]
VBCrLf A carriage return and line feed [Chr(13) + Chr(10)]
vbBack A backspace character [Chr(8)]
vbLf A linefeed [Chr(10)]
vbNewLine A platform-specific new line character, either [Chr(13) + Chr(10)] or [Chr(13)]
vbNullChar A null character of value 0 [Chr(0)]
vbNullString A string of value 0 [no Chr code]; note that this is not the same as “”


Dec 2 2008   12:55AM GMT

Very simple encryption example with VBScript



Posted by: Jerry Lees
encryption, VBScript, 3des, string, encrypt, Toolkit, String manipulation, decrytption, mid, StrReverse, Reverse Strings, decrypt, RSA

I previously mentioned a routine that I wrote to encrypt a string. Now, before the security folks look at the code… understand this:

This is intended only to obscure a string from a casual prying eye. It is NOT intended to be a replacement for true encryption like 3DES and RSA encryption. Please do NOT assume this routine is in any way secure or uncrackable. It is intended to only be an exercise in working with strings and is only as secure as the price you have paid for it. Nothing. ;-) Furthermore, it is provided as-is without warranties and by using it you agree that all risk from it’s use is transferred to you.

….Now that we’ve gotten the unpleasant legal disclaimer out of the way… Lets discuss the code!

Essentially, The code uses a variable length key to obscure the original string by iterating through the string you want obscured and adding the ASCII value of the character at each position of the original string with the ASCII value of a rotating “key character” in the key provided to generate a new ASCII value. This new ASCII value is then converted to a character and added to the newly encrypted string. The obscured string is further obscured by the fact that the original string is reversed prior to being changed. 

This key position changes after each character in the original string is obscured. The result is the key is iterated through sequentially as the original string is encrypted and when the end of the key string is encountered the iteration through the key string is started again from the beginning of the key string until the original string is completely encrypted.

This process works because the ASCII values in the typical string and the typical key string when added together do not exceed 255. (The highest possible ASCII character) Essentially, Strings and Keys with ASCII values higher than 126 should not be used or the result could be unpredictable– or worse yet, an unencryptable string.

Now that I’ve explained a bit about the premise… Lets look at the code!

Option Explicit

Dim temp, key

temp = “Now is the time for all good men To come To the aid of their fellow countrymen.”
key = “huasHIYhkasdho1″

temp = Encrypt(temp,key)
WScript.Echo temp
temp = Decrypt(temp,key)
WScript.Echo temp

Function encrypt(Str, key)
 Dim lenKey, KeyPos, LenStr, x, Newstr
 
 Newstr = “”
 lenKey = Len(key)
 KeyPos = 1
 LenStr = Len(Str)
 str = StrReverse(str)
 For x = 1 To LenStr
      Newstr = Newstr & chr(asc(Mid(str,x,1)) + Asc(Mid(key,KeyPos,1)))
      KeyPos = keypos+1
      If KeyPos > lenKey Then KeyPos = 1
 Next
 encrypt = Newstr
End Function

Function Decrypt(str,key)
 Dim lenKey, KeyPos, LenStr, x, Newstr
 
 Newstr = “”
 lenKey = Len(key)
 KeyPos = 1
 LenStr = Len(Str)
 
 str=StrReverse(str)
 For x = LenStr To 1 Step -1
      Newstr = Newstr & chr(asc(Mid(str,x,1)) - Asc(Mid(key,KeyPos,1)))
      KeyPos = KeyPos+1
      If KeyPos > lenKey Then KeyPos = 1
      Next
      Newstr=StrReverse(Newstr)
      Decrypt = Newstr
End Function


Nov 28 2008   5:07PM GMT

Reversing a String with VBScript… the easy way!



Posted by: Jerry Lees
VBScript, String manipulation, StrReverse, Reverse Strings

In a previous post, entitled Reversing a string with VBScript using the mid function, I shared a piece of code with you that reversed a string. It was simple and effective… but it’s not the piece of code I ended up using in my encryption code.

I found a much easier function built right into VBScript! The StrReverse function! It’s quite simple to use… pass it a string and it returns back to you a reversed version of that string. Here is the code I posted earlier– only using the StrReverse function:

Option Explicit

Dim MyStr, char, NewStr, x, y

MyStr = “Reverse Me!”

NewStr = StrReverse(MyStr)

WScript.Echo NewStr


Nov 26 2008   2:43PM GMT

Reversing a string with VBScript using the mid function



Posted by: Jerry Lees
VBScript, String manipulation, mid

As a part of the VBScript encryption I mentioned working on I needed to find a way to reverse a string. Doing this yielded many possibilities, but this piece of code seemed to work the best (and was the simplest) out of all the pieces of code I came up with to reverse a string.

It’s simple, but effective and you could easily wrap a function statement around it to create your own function to reverse a string. Here is the code:

Option Explicit

Dim MyStr, char, NewStr, x, y

MyStr = “Reverse Me!”

y = Len(MyStr)
For x = y To 1 Step -1
     char = Mid(MyStr,x,1)
     NewStr = NewStr & char
Next

WScript.Echo NewStr


Oct 1 2008   2:06PM GMT

Easy string manipulation with VBScript using the Replace Function



Posted by: Jerry Lees
VBScript, Functions, VBScript Functions, Replace, String manipulation

Recently, I had a situation at the office where I needed to check a web.config file for the existence of a setting. Not a hard problem– just open the file up an look for it, however, that becomes a little harder when your talking about 30 servers. So, I scripted it using the replace function!

The replace function in VBScript is an invaluable tool in very many scripts, especially when you want to simply check for the existence of a string in another string or completely replace a string with another string. Its simplest syntax is fairly straightforward as well, it is:

NewString = Replace (Searchedstring, FindString, Replacewithstring)

This one function allows you to completely change a file by searching for the value in the string(s) read into a variable then saving the new variable into the file. Or you can do a comparison on the new variable with the old one to see if they are equal, if they aren’t the string was found and replaced.

All in all, Replace is an invaluable tool to put in your scripting arsenal.