Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove characters from a string 1

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
Hi everyone,

I've been playing with a string which is built from calls to DB fields. In this example, the variable "trimmed" is a bit of text that can be imported from a form field or from a database field.

<%
trimmed = Replace(Trim(Left(objRSPortables(&quot;ProductDescription&quot;),165)),&quot;'&quot;,&quot;’&quot;,1,-1,vbTextCompare)
If Len(trimmed) < 165 Then
Description.Write(trimmed)
Else
Description.Write(trimmed & &quot;... (more)&quot;)
End If
%>

My problem is that mostly all of the time, the &quot;ProductDescription&quot; field will be larger than 165 characters, so the script appends the &quot;... (more)&quot; after the string. This more often than not winds up with ugly truncated strings that cutoff in the middle of words such as:

<!--
As the island’s Carabao population continues to grow on U.S. Navy property in Fena - a population that has caused many a problem to the ecosystem there - the Navy al... (more)
-->

Is there a way I can have the script kill off any additional characters (hopefully in blocks, so I could do it by whole words), so the strings can be a bit more presentable?

Thanks! :)
 
this should do, let me know

Code:
<%
trimmed = Replace(Trim(Left(objRSPortables(&quot;ProductDescription&quot;),165)),&quot;'&quot;,&quot;’&quot;,1,-1,vbTextCompare)
If Len(trimmed) < 165 Then
   Description.Write(trimmed)
Else
   Position1=instr(trimmed, &quot; &quot;)
	Position2 = 2
	while Position1 > 1
		if Position2 > 1 then
			Position1=Position1+1
			Position2=instr(Position1, trimmed, &quot; &quot;)
		else
			Position2 = Position1
			Position1=0
		end if
	wend
	Position2 = Position2-1
	trimmed = left(trimmed, Position2)
	trimmed = trimmed & &quot; ... (more)&quot;
	description.write(trimmed)
end if
%>
 
Hi again pgferro...I wrote my complete script...send me your mailto at jason@kuam.com and I'll send it to you privately. I actually incorporated your help as logic for a much larger script to use for external site publishing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top