Asp Strings / String Manipulation
Len() | Right(),Left() | Instr() | InstrRev() |
Mid() | Replace() | Trim(),RTrim(),LTrim() |
ASP Provides a
set of functions that allow you to chop,prune and order strings in any way you
like.Below I explain
each one individually.
Asp – Length of a string
LEN function
returns the length of a string ie. the number of characters in a string.This
function has only one argument namely the string that you are measuring.The
syntax is:
Len(string)
Eg:
<%
Dim strtest,varlength
strtext=”Howareyoudoing?”
varlength=Len(strtext)
response.write(varlength)
%>
Result
is 15
Asp – Right() and Left() functions
Returns a given
number of characters from the beginning and end of a string.The syntax is :
Right(string,NumberOfCharecteres)
Left(string,NumberOfCharecteres)
Eg:
<%
Dim strtest,str1,str2
strtext=”Howareyoudoing?”
str1=Right(strtext,6)
str2=Left(strtext,3)
response.write(“The right six characters
in the string is ” & str1 & “<br>”)
response.write(“The left three characters
in the string is ” & str2)
%>
The result is
:
doing?
How
Asp – InStr Function
The syntax is:
Instr(string,StringToBeLocated)
This returns the
position at which the StringToBeLocated was found.This returns
0 if StringToBeLocated is not found or if StringToBeLocated is
zero-length.This returns null if string or StringToBeLocated is
null.
Eg:
<%
Dim strtest,intfound
strtext=”Howareyoudoing?”
intfound = instr(strtest,”you”)
if intfound>0 then
response.write(“String Found”)
Else
response.write(“String not found”)
End if
%>
Instr is case
sensitive.ie if you search for “You” instead of “you” it returns 0.
Asp – InStrRev()
This is same as
InStr(), except starts from right side of string
Asp – mid() Function
This function
returns the requested number of characters from a string.This works along the
same principle as Right() or Left() , but it takes an extra parameter.
The syntax is:
Mid(String, CharToStartWith,
NumberOfCharectersToBeExtracted)
Eg:
<%
Dim strtest,str1
strtext=”Howareyoudoing?”
str1 = mid(strtext,4,3)
response.write(str1)
%>
Result
is : are
if
NumberOfCharectersToBeExtracted is left out, Mid will return all of the characters
starting from CharToStartWith.
Asp – Replace() Function
The syntax is:
Replace(String, ReplaceWhat,ReplaceThis)
This returns a
string with ReplaceWhat replaced with ReplaceThis.
Eg:
<%
str1 = “This#is#an#example”
Response.Write(“Replace # with a space = ” & Replace(str1, “#”, ” “))
%>
Also we can specify
replacement should start from where and how many times replacement should be
done.
<%
str1 = “This#is#a#detailed#example#”
Response.Write(“Replace # with a space = ” & Replace(str1, “#”, ” “,8,1))
%>
Top
Asp – Trim(),LTrim() , RTrim() Functions
The function LTrim()
removes spaces from the left hand side of the string, RTrim() removes spaces
from right hand side of a string and Trim() function combines the two,removing
all the spaces from right and left end of the string.