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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use functions to separate some text from a string

Status
Not open for further replies.

jodie

MIS
May 18, 2001
30
US
I have a string that reads as below. Out of this entire string, I want to separate the value of whatever is in between the \ \ i.e. sbp in this case and store that in another variable. The value in between the \ is not always the same # of characters, this varies. Could someone please tell me how to do this. I was able to remove the first 2 characters using the LEft function but could not do the rest.

.\sbp\B262591.CMI:Student Name: JOHN BRYAN

Thanks
 
This is the concept of using the split() function, though there are other ways too such as using the inStr() function:

Dim myText, pieces(2) 'only expecting 3 elements (0,1,2)
myText=".\sbp\B999999.CMI:Student Name: JOHN SMITH"
pieces=spilt(myText,"\",3) 'process only 3 elements
'notice that pieces array starts with 0, so you want 1
response.write pieces(1)

dbMark
 
Mark,

Just one more problem. The length of the word between the slashes won't always be 3 characters, is there a general way of saying this so it grabs that text whether the length may be 3, 4 or 5 or otherwise.
thanks
 
pieces=spilt(myText,"\",3) 'process only 3 elements

The 3 in this line does not represent the length, it specifies the maximum number of substrings or elements to get. From your example, it appeared that there would only be 3 substrings, one before the first backslash, one between them and one after them. So only those 3 substrings are put into the array elements and if there are any more, then they're ignored. The second element is the one you want since it will contain everything between the backslashes: pieces(1).

Are you familiar with split()? Check the devguru website to see its parameters and examples.
 
I tried this code, however I get an error for the third line which is below. The error I get is 'type Mismatch' on this line.
Is this because pieces is declared as an array and we are using it like a variable to store a value.




pieces=split(myText,"\",3) 'process only 3 elements
 
Are there always only 2 backslashes (\) ?

In that case the solution from dbMark should work.

Try adapting his code like this:

Dim myText, pieces
myText=".\sbp\B999999.CMI:Student Name: JOHN SMITH"
pieces=split(myText,"\")
response.write pieces(1)
 
Dim myText, pieces(2) 'only expecting 3 elements (0,1,2)
myText=".\sbp\B999999.CMI:Student Name: JOHN SMITH"
pieces=split(myText,"\") 'process only 3 elements
'notice that pieces array starts with 0, so you want 1
response.write pieces(1)

This is the exact and only code I have in my VBscript. I want to make sure this works before I define a file to write it to etc. Could that be the problem. Shouldn't it atleast process without errors. When I double click my VBscript with this code, it tells me "Type Mismatch"...Line 3
 
I got it. I declared pieces without the (2) and I got it to work. I got it to work when I used the code below.
thanks much for your help.

Dim sData
Dim sTargetText

sData = ".\sbp\B262591.CMI:Student Name: JOHN BRYAN"

' Splitting sData returns an array (0-2) of 3 elements, so element
' one contains the piece of the data you're after:
sTargetText = Split(sData, "\")(1)
objOutputFile.write sTargetText
 
I guess the problem is that I dimensioned a static array and split() only wanted to work with dynamic arrays. Technically, here are the differences for Dim:
Code:
'Dim is not required unless you use Option Explicit
Dim pieces(2) ' static array
Dim pieces()  ' dynamic array allows use of ReDim pieces(n)
Dim pieces    ' variable
dbMark
 
Psuedo code solution.
Use the mid function from left to right.
After you hit the first "\" copy each character into an
array or declared variable.
When you hit the next "\" your done.
adding 1 to a counter each time you copy a character will tell you how long the data is.
Enjoy Bob
 
Always more than one way to do something:
Code:
Dim myText, myPart
myText=".\sbp\B999999.CMI:Student Name:    JOHN  SMITH"
myPart=mid(myText,inStr(myText,"\")+1)
myPart=left(myPart,inStr(myPart,"\")-1)
objOutputFile.write myPart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top