String Functions/Manipulations
1. Determine the lenght of a string
<% Dim strMyString strMyString="hello world" response.write len(strMyString) %>
Output: 11 including spaces.
2. Extracting substring from a String
<% Dim strMyString strMyString="hello world" response.write left(strMyString,5) %>
Output: hello
The left function takes two arguments. The string first and an integer that determines how many character spaces the function will stop looking from the left. It is actually self explanatory ^^. The other similar function Right does the same thing but of course it start looking on the right side of the string.
<% Dim strMyString strMyString="hello world" response.write Right(strMyString,5) %>
Output: world.
3. Setting string to High/Low Caps.
<% Dim strMyString strMyString="hello world" response.write Ucase(strMyString) %>
Output: HELLO WORLD. Otherwise
<% Dim strMyString strMyString="hello world" response.write lcase(strMyString) %>
Output: hello world. Ucase=High CAPS. lcase=Low Caps.
4. Replacing a particular substring in a string.
<% Dim strMyString,strNewString strMyString="hello world" strNewString=Replace(strMyString,"world","earth") response.write strNewString %>
Output: hello earth.
As you’ve guessed, The replace function takes three non optional arguments. First the source string,second the substring the you want to search for and third the replacement string. In this example,you actually tell the program to look for the word “world” and replace it with the word “earth.” if the function successfully find the word “world” in a string it will replace it with the replacement word which happens to be “earth.” Hence, hello earth. You will wanna used this function to filter bad words and replace them with ** characters like you are sensoring them ^^.
5. Using the inStr function to check if a substring exist in a string.
<% Dim strMyString strMyString="hello world" response.write inStr(strMyString,"hello")<> 0
%>
Output: True.
The inStr function takes two non optional arguments. First the source string and second the substring that you want to check for. The inStr function if successful will return an integer that represents the position of the first character of the string that you want to check. In this case, hello.
<% response.write inStr(strMyString,"hello") %>
will display 1.
Since h is the first character of the string hello world. Otherwise it will return 0 means that hello was not found in the string. What i did was create a condition that check if the returned integer is not equal to 0 (I am looking for that integer that represents the first character of the substring hello ^^) fortunately hello was found and the function returns 1. And we all know that 1 is not equal to 0 (1<> 0) that leads us to True. If it’s true then you substring was found in a string.
In the next article, i will try to show you how amazing it is to put these stuff together and put them to work ^^
Posted in ASP Programming
![[del.icio.us]](http://genusproject.com/wp-content/plugins/bookmarkify/delicious.png)
![[DotNetKicks]](http://genusproject.com/wp-content/plugins/bookmarkify/dotnetkicks.png)
![[Digg]](http://genusproject.com/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://genusproject.com/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://genusproject.com/wp-content/plugins/bookmarkify/google.png)
![[MySpace]](http://genusproject.com/wp-content/plugins/bookmarkify/myspace.png)
![[Squidoo]](http://genusproject.com/wp-content/plugins/bookmarkify/squidoo.png)
![[StumbleUpon]](http://genusproject.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://genusproject.com/wp-content/plugins/bookmarkify/technorati.png)
![[Windows Live]](http://genusproject.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://genusproject.com/wp-content/plugins/bookmarkify/yahoo.png)

April 19th, 2008 at 7:01 pm
nice tutorial…this one will help me in my asp application (”i got an idea!”). keep up! god bless..
April 20th, 2008 at 6:55 am
glad to be of help.