how to split a split string
Use the function INSTR with the LEFT and or MID functions as in
DIM strVariable, splitColon, arrSplitComma
strVariable="Specification:4 rosequartz, 2 haematite,1 amethyst, Name:
arrSplitColon = LEFT(strVariable, INSTR(strVariable,":")-1 )
The variable arrSplitColon will be set to "Specification"
What is happening is the INSTR function returns the position of the
FIRST occurance of the search string i.e. : as an integer. Zero
would be returned if the search string wasn't present. The LEFT
function returns the first so many characters from the string
strVariable in this case determined by the INSTR return value.
I am pretty sure an error will occur if the INSTR returns zero in the
above line as the LEFT function doesn't like zero as the length so to
protect your program do
intPos = INSTR(strVariable,":")
IF intpos > 1 then ' Test the result of the search
arrSplitColon=LEFT(strVariable, intPos -1)
IF intPos = 1 ' i.e. the first character was a colon
arrSplitColon = ""
arrSplitColon = "No colon found"
Just to make life a but more interesting (i.e. harder!) I see there
are 2 colons in the sample you provided. To find both occurances do
dim intPos1, intPos2
intPos1 = INSTR(strVariable,":") finds the first colon
intPos2 = INSTR(intpos1 + 1, strVariable,":") finds the second
intPos 1 will be set to 14 and intPos2 to 62 assuming I counted
The use of an integer as the first parameter i.e. intpos1 + 1 tells
INSTR the first position to search from, if this is omitted the first
character is assumed.
To get the value of "Peter Cartwright" from this string use
to get everything between the :'s
MID(strVariable, intpos1 + 1, intpos2 - intPos1)
The search string could be "Specification" in which case INSTR would
return a value equating to the postion of S (i.e. 1). Note INSTR is
not limited to one character search strings.
Hope this helps
> to breaking up a string, without success. Could someone care to put me
> right on this?
> The following threw up two errors (or is it really one?).
> 'Expected end of statement line 9' that is msgBox arrSplitColon, and
> 'Type mismatch on splitString' when I clicked the hperlink.
> <script language="vbscript">
> Sub splitString
> dim strVariable, splitColon, arrSplitComma
> strVariable="Specification:4 rosequartz, 2 haematite,1 amethyst, Name:
> Peter Cartwright"
> msgBox arrSplitColon
> msgBox arrSplitComma
> End Sub
> <a href="#" onclick="splitString">split the string</a>