BeWebmaster

ASP SPLIT, REPLACE and JOIN String Functions

Ad

LearnAsp.com

The various string functions can be used interchangeably often. We will write the same script using SPLIT, REPLACE and JOIN to demonstrate all 3 functions and how they often are used for similar tasks. However, your programming situations may be especially suited for 1 out of 3 of the functions.

SPLIT(string,delimiter)
is a function that returns an array with as many elements as are separated by delimiters within the string.

The user can enter a state or multiple states. We write the program utilizing SPLIT to demonstrate how this can be  used.

statesplit.asp

<html><head>
<title>states.asp</title>

</head><body bgcolor="#FFFFFF">
<form action="statesplitrespond.asp" method="post">
Enter State (or many states separated by <b>,<b><p>
<input NAME="states" size="40"><p>
<input type="submit" value="Get The Publishers!">
</form>
</body></html>

statesplitrespond.asp

<!--#include virtual="/learn/test/lib_dbtable.asp"-->
<html><head>
<title>statesrespond.asp</title></head>
<body>
<%
mySQL="select * from publishers " & whereclause
myDSN="DSN=student;uid=student;pwd=magic"
public allstates
tempinput=request("states")
allstates=split(tempinput,",")

maxcounter=ubound(allstates)

whereclause=" where state='" & allstates(0) & "'"
FOR counter=1 TO maxcounter
   thisstate=allstates(counter)
   whereclause=whereclause & " OR state='" & thisstate & "'"
NEXT
mySQL=mySQL & whereclause
Call Query2Table(mySQL,myDSN)
%>

</body></html>

REPLACE(string,oldvalue,newvalue)
results in a string that has all occurences of oldvalue replaced with a newvalue.

The user can enter a state or multiple states. We write the program utilizing REPLACE to demonstrate how this  function can be used.

statereplace.asp

<html><head>
<title>statereplace.asp</title>

</head><body bgcolor="#FFFFFF">
<form action="statereplacerespond.asp" method="post">
Enter State (or many states separated by <b>,<b><p>
<input NAME="states" size="40"><p>
<input type="submit" value="Get The Publishers!">
</form>
</body></html>

statereplacerespond.asp

<!--#include virtual="/learn/test/lib_dbtable.asp"-->
<html><head>
<title>statesrespond.asp</title></head>
<body>
<%
mySQL="select * from publishers " & whereclause
myDSN="DSN=student;uid=student;pwd=magic"

tempinput=request("states")

whereclause=" where state='" & tempinput
whereclause=replace(whereclause, ",","' OR state='")
whereclause=whereclause & "'"

mySQL=mySQL & whereclause
response.write mySQL
Call Query2Table(mySQL,myDSN)
%>

</body></html>

JOIN(array,delimiter)
takes an array and converts it to one string with delimiters.

The user can enter a state or multiple states. We write the program using the JOIN function to demonstrate how this function can be used.

statejoin.asp

<html><head>
<title>statejoin.asp</title>

</head><body bgcolor="#FFFFFF">
<form action="statejoinrespond.asp" method="post">
Enter State (or many states separated by <b>,<b><p>
<input NAME="states" size="40"><p>
<input type="submit" value="Get The Publishers!">
</form>
</body></html>

statejoinrespond.asp

<!--#include virtual="/learn/test/lib_dbtable.asp"-->
<html><head>
<title>statejoinrespond.asp</title></head>
<body>
<%
mySQL="select * from publishers "
myDSN="DSN=student;uid=student;pwd=magic"
public allstates
tempinput=request("states")
allstates=split(tempinput,",")

whereclause=" where state='" & JOIN(allstates, "' OR state='") & "'"

mySQL=mySQL & whereclause
response.write mySQL
Call Query2Table(mySQL,myDSN)
%>

</body></html>