cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

How do I create a String object in scripting?

CarlDreher
1-Newbie

How do I create a String object in scripting?

I'm retrieving a list of File objects and want to manipulate the file name. However, whenever I try to do any String manipulations with myFile.Name, I get a VBScript error that it isn't a String object. So that lead me to start looking at Strings and getting absolutely nowhere.

For example, Mathcad won't let me do any of the following:

1. Set str = New String("foo") (All of these generates an "expected end of statement" error. Huh?)

Dim str = String("foo")

Dim str = new String("foo")

2. str = "foobar"

index = str.lastIndexOf("foo") (Complains "object required", which I presume means it doesn't think str is a String object.)

3. str = "foobar"

index = LastIndexOf(str, "foo") (Error is "type mismatch")

This is driving me nuts! How do I create a String object, especially from the file.Name method?

5 REPLIES 5

Errors are:

1. "String" don't exist as an object (actually the word is a function), are a data type. So you can assign with Set or New. The error "expected end of statement" is because can't use Dim with assignation in vbscript.

2 & 3. "foo" isn't an object nor str.

I think that what you want is the funciton Len(str) which return the string length.

VBScript is a very small subset of the basic language. You can try to find in your computer the file "script56.chm" or try in microsoft web to download it.

Regards. Alvaro.

RichardJ
19-Tanzanite
(To:CarlDreher)

VBScript or JScript? You can write a scripted component in either, bt not a mixture of the two.

The code you have in 2 and 3 is JScript. In VB a string is a variable subtype, not an object.

I have many examples of how to do this kind of stuff. What exactly are you trying to do?

OK, I eventually found what I was looking for thanks to the link to the script56.chm.

As pointed out, VBScript doesn't handle strings as proper String objects. (Obviously, VBScript was written a long time ago!)

So, to manipulate the file name (obtained from a proper File object), I must do this:

Dim myString

myString = myFileObject.Name

Then I can use various string functions that exist separately, such as

myIndex = InStr(myString, "*.txt")

to get the index of the *.txt suffix.

RichardJ
19-Tanzanite
(To:CarlDreher)

Yes, VBscript is a little old.

If you tell me what you are trying to achieve though, I may have something already written that could save you a lot of work.

Thanks, but I've got it done. Once I realized I had to use the various functions that work on string-like variables, it was easy.

Top Tags