Blogsite that talks about web Programming experiences,techniques and ideas for ASP,PHP,ASP.NET and Javascript.

Help get peace of mind knowing that everything is managed for you with Windows Live OneCare—virus and spyware scanning, firewalls, tune-ups, file backups, the whole nine yards. And it's all delivered to you in a smooth, hassle-free package. Download the 90 day free trial

String Functions/Manipulations

April 1st, 2008 by Roy L. `dshiznitz Besiera

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 ^^

[del.icio.us] [DotNetKicks] [Digg] [Facebook] [Google] [MySpace] [Squidoo] [StumbleUpon] [Technorati] [Windows Live] [Yahoo!]

Posted in ASP Programming

2 Responses

  1. juno

    nice tutorial…this one will help me in my asp application (”i got an idea!”). keep up! god bless..

  2. Roy L. Besiera

    glad to be of help. :)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.