|
String Functions by Charles Carroll
String functions are very useful for parsing
data from ASCII files, formatting output in complex ways and parsing form input. The
following scripts provides a sample of some crucial VBScript string functions in action.
The string functions demonstrated are:
Instr(string,searchstring)
returns a numeric position where search string was found
Mid(string,start,length)
chops string at a start position for a fixed number of characters.
Mid(string,start)
results in a string that has all characters before startpos removed.
Trim(string)
removes all spaces from a string.
filename=/learn/test/citystatezip.asp
<html><head>
<title>citystatezip.asp</title>
</head><body bgcolor="#FFFFFF">
<Form action = "citystatezipRespond.asp" method="post">
Enter City <b>,</b> State Zip<p>
<Input NAME="CSZ" size ="40"><p>
<Input type="submit" value="Here is my Data!">
</form>
</body></html>
The user can enter a city, state and Zip, i.e.
Rockville, MD 20849
San Fransisco, CA xxxxx
Pike's Peak, CO xxxxx
and the program can use these functions to separate the data.
filename=/learn/test/citystateziprespond.asp
<html><head>
<title>citystateziprespond.asp</title>
</head><body>
<%
alldata=request("csz")
IF instr(alldata,",")=0 THEN%>
You need a comma<br>,<br>between city and state!<p>
<%response.end
END IF
findcomma=instr(alldata,",")
city=mid(alldata,1,findcomma-1)
leftover=trim(mid(alldata,findcomma+1))
findspace=instr(leftover," ")
state=mid(leftover,1,findspace)
leftover=mid(leftover,findspace+1)
zip=leftover
response.write "city=" & city & "<br>"
response.write "state=" & state & "<br>"
response.write "zip=" & zip & "<br>"
%>
</body></html>
|