Codefixer - ASP tutorials, resources and software
ASP.NET 3.5/2.0 Hosting – Click Here
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Monday 12 May
MembersPro PayPal - ASP Membership software

ASP.NET 3.5 Hosting – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

Cut the String Length

The function below will return a string with the number of characters that you set as a parameter.

<%
'Our function Shorten can accept 2 parameters
'The string and the length of characters to shorten the string too
Function Shorten(sString, sLength)
'Make sure length of the string is more than the number of characters to return
If Len (sString) > sLength Then
'Add four full stops so that the visitor knows the string has been abbreviated
         Shorten = Left(sString,sLength) & "...."
Else
'If the string is shorter than the number of characters allowed then don't shorten
         Shorten = sString
End If
End Function
%>

Below is sample code that will retrieve the title for each news article in our database 'News.mdb'. We will use the function Shorten to limit the length of the display title to 20 characters and add four full stops. The visitor would see a shortened version of the title that would act as a teaser. Although our code below doesn't do it you could hyperlink the title and pass through the ID of the record and then display the title and news article in full on a separate page.

Example 1:

<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Cut the length of the String</title>
</head>
<body>
<%
Function Shorten(sString, sLength)
If Len (sString) > sLength Then
         Shorten = Left(sString,sLength) & "...."
Else
         Shorten = sString
End If
End Function
'Declare your variables
Dim oConnection, oRecordset
Dim sSQL, sConnString, sNewsTitle

'declare SQL statement that will query the database
sSQL="SELECT * FROM tblNews"

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("News.mdb")


'create an ADO connection and recordset
Set oConnection = Server.CreateObject("ADODB.Connection")
Set oRecordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
oConnection.Open(sConnString)

'Open the recordset object, execute the SQL statement
oRecordset.Open sSQL, oConnection

'first of all determine whether there are any records
If oRecordset.EOF Then
Response.Write("There are no titles.")
Else
'If there are records then loop through the fields
Do While Not oRecordset.EOF
sNewsTitle= oRecordset("Title")
'Call our function Shorten
'pass in our variable 'sNewsTitle' and assign returned value to variable
sNewsTitle=Shorten(sNewsTitle,20)
Response.Write sNewsTitle
Response.Write "<br>"  'insert a line break
'move on to the next record
oRecordset.MoveNext
Loop
End If

'close the connection and recordset objects and free up resources
oRecordset.Close
oConnection.Close
Set oRecordset = Nothing
Set oConnection = Nothing
%>

</body>
</html>





If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.

ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software