how to split a split string 
Author Message
 how to split a split string

I've just come from Javascript so have been trying the same approach
to breaking up a string, without success. Could someone care to put me
right on this?

Peter

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.
<html>
<head>
<title></title>
<script language="VBScript">
Sub splitString
dim strVariable, splitColon, arrSplitComma
strVariable="Specification:4 rosequartz, 2 haematite,1 amethyst, Name:
Peter Cartwright"
arrSplitColon=split(strVariable,":")
msgBox arrSplitColon[0]
arrSplitComma=split(arrSplitColon[1],",")
msgBox arrSplitComma[0]
End Sub
</script>

</head>
<body>
<a href="#" onclick="splitString">split the string</a>
</body>
</html>



Sun, 20 Mar 2005 14:51:01 GMT  
 how to split a split string
Peter,

I did the following changes to get the script running:

1. Modified the second variable in the dim statement to arrsplitColon
2. Replaced all sqzared brackets [] by round brackets ().

Fred

Quote:

> I've just come from Javascript so have been trying the same approach
> to breaking up a string, without success. Could someone care to put me
> right on this?

> Peter

> 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.
> <html>
> <head>
> <title></title>
> <script language="vbscript">
> Sub splitString
> dim strVariable, splitColon, arrSplitComma
> strVariable="Specification:4 rosequartz, 2 haematite,1 amethyst, Name:
> Peter Cartwright"
> arrSplitColon=split(strVariable,":")
> msgBox arrSplitColon[0]
> arrSplitComma=split(arrSplitColon[1],",")
> msgBox arrSplitComma[0]
> End Sub
> </script>

> </head>
> <body>
> <a href="#" onclick="splitString">split the string</a>
> </body>
> </html>



Sun, 20 Mar 2005 19:45:49 GMT  
 how to split a split string
Peter

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:
Peter Cartwright"

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

dim intPos
intPos = INSTR(strVariable,":")
IF intpos > 1 then   '  Test the result of the search
   arrSplitColon=LEFT(strVariable, intPos -1)
ELSE
   IF intPos = 1    ' i.e. the first character was a colon
      arrSplitColon = ""
   ELSE
      arrSplitColon = "No colon found"
   END IF
END IF

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
this

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
correctly!!!

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
MID(strVariable, intPos2)
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

Duncan

Quote:


> I've just come from Javascript so have been trying the same approach
> to breaking up a string, without success. Could someone care to put me
> right on this?

> Peter

> 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.
> <html>
> <head>
> <title></title>
> <script language="vbscript">
> Sub splitString
> dim strVariable, splitColon, arrSplitComma
> strVariable="Specification:4 rosequartz, 2 haematite,1 amethyst, Name:
> Peter Cartwright"
> arrSplitColon=split(strVariable,":")
> msgBox arrSplitColon[0]
> arrSplitComma=split(arrSplitColon[1],",")
> msgBox arrSplitComma[0]
> End Sub
> </script>

> </head>
> <body>
> <a href="#" onclick="splitString">split the string</a>
> </body>
> </html>



Sun, 20 Mar 2005 23:06:21 GMT  
 how to split a split string
Quote:

> Peter

> Use the function INSTR with the LEFT and or MID functions as in

Thanks all for the help.  Glad you mentioned that several characters
can be used in a search.  I had thought that it should be possible,
because of it's obvious utility. Just chose the wrong function to use
it with. :)


Thu, 24 Mar 2005 02:49:39 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Best way to split string - mid() or split()?

2. get the total split words value by using the command split

3. Split Bars & Split Windows

4. Server side .split() working differently to client side .split()

5. Splitting arrays. X=split(split(y,"~"),"*") How Do i do this

6. Split string by regular expression (ie. date)

7. Splitting Strings

8. String Split

9. Newbie wants to split up strings

10. Splitting a string into each character..

11. SPLIT A LONG STRING TO PUT IN A MULTILINE BOX

12. SPLIT A LONG STRING

 

 
Powered by phpBB® Forum Software