ASP Trim Text String
- Started
- Last post
- 9 Responses
- PeterNorf
I have a row, in a table in SQL that a news story is inserted into. On a Home page I want to show the picture, Title and first 100 characters from the newsstory, that news story comes from a recordset item:
News.Fields.Item("body").ValueI have looked at the Trim() & Replace() functions and have not figured it out. Am I looking in the right place?
I assume I can create a variable that copys the first 100 chars from a recordset.item??
- mitsu0
um, do you mean:
left(rs.fields("body").value, 100)
- nonakedflame0
function trimDescription(p_String, p_Length) {
/*
if it the length is greater then the no of words in the article, return the string
*/if (p_Length > p_String.length) {
return p_String;
}/*
Split the string up on the spaces
*/splitString = p_String.split(" ");
var trimmedString = "";
for (a=0; a < p_Length; a++) {
trimmedString += " " + splitString[a];
}
return trimmedString;
}
Usage -
Top of page ---
In table cell
note : this is a very cheap & nasty way of doing this... does not account for leading and/or trailing spaces...
Hope this helps (works for me!)
- nonakedflame0
Oops post missed some code out... was in ASP Javascript anyhow... no good if your using VBScript....
- PeterNorf0
Thanks Mitsu!
- mitsu0
np
- PeterNorf0
I know this is pushing it, but is there a way that I can find the last period after 100 characters? So it doesn't end in the middle of the word or middle of sentance?
- mitsu0
piece of cake:
Response.Write(left(mystring, instr(100,mystring, ".")))
only problem is that you don't know when the first occurance of a period will be after the 100th character, so i'd recommend getting the lenth of the string and if it's over 100, just add "..."
- k20
bookmark http://msdn.microsoft.com/librar…
you might want to look at using local arrays to hold the recordset instead of the ado object. nasty on resources and speed if it's a high traffic site (or anytime compared to a local array hehe).
- PeterNorf0
Sorry about the late response guys. I have seen some code that uses arrays to do this, they are a bit harder to swollow for a novice like myself. This will do for now. Thanks again!
Especially Mitsu for the quick responses.