PDA

View Full Version : Displaying first 25 char of Desc String... and a bit more


murgeltd
06-02-2006, 10:15 AM
Hi there,

I want to be able to display, say, the first 25 characters of a description string. But here's the twist..

..displaying the first 25 characters is easy but I want to be able to chop the sentence off at the end of a word, or the first sentence (ending with a full stop).

I will then create the classic ...more link people can click to see the actual record.

I guess it will be someting like...
while not end of sentence/word & below 25 char
display desc
else
???

Is there a quick easy coding way of testing for end of word or sentence?

Any help is appriceiated.

Andy

miranda
06-02-2006, 03:28 PM
Use the InStr Method to look for a space (" ") just start at 25th character

Left(theString,InStr(25,theString," "))

of course you must make sure the string theString is longer than 25 characters to begin with.
to check that use

If Len(theString) > 25 Then Left(theString,InStr(25,theString," "))

murgeltd
06-05-2006, 11:49 AM
I have added the following code as per your example...

<%
Dim Desc
Desc = rs("newsDesc")
If Len(Desc) > 25 Then
Left(Desc,InStr(25,Desc," "))
response.Write(Desc)
Else
response.Write(Desc) %></br>
<% End If %>

... but get the following error

Cannot use parentheses when calling a Sub

/newsxchange/news.asp, line 60

Left(Desc,InStr(25,Desc," "))

What am I doing wrong? Can anyone help?

Thanks in Advance

Andy

mehere
06-05-2006, 05:17 PM
change it to this:
Desc = Left(Desc,InStr(25,Desc," "))

degsy
06-06-2006, 02:21 PM
What database are you using?
There are some functions that you can use in the SQL query to output the correct info. Same logic, just one less operation.

ghell
06-06-2006, 09:23 PM
substring(source, start[, length]) [or something along those lines] is standard sql for it that will be in any dbms. it is at least in SQL92 and most likely in the original SEQUEL. CharIndex(field, " ", 25) is in SQL Server (its analagous to InStr() in ASP)

mehere is right though, if you assign the returned value to a variable it will be treated as a function not a sub. if you want to use subs you have to use this:

Call MySub(someparameter, someotherparameter)
or
MySub someparameter, someotherparameter

The difference between a function and a sub is a sub doesn't return anything (and this does so its definately a function)

i prefer using the Call line but most people prefer not to use the parenthesis.. either way its a function so you should just put an assignment before it as mehere said :thumbsup: