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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Mid and/or InStr functions to dipslay partial results of a field 7

Status
Not open for further replies.

gchaves

Programmer
Oct 27, 2003
105
US
Hello,

I am building a calendar listing using ASP and VBScript. The calendar contains a field that list a description of an event, which could be more than 255 characters long (Access 2000 database...set as a Memo datatype). I only want to show the first 275 characters of the event description, but want to make sure that I always end on a space, instead of mid-word. Would I need to use the Mid function or the InStr function or both?

I can use the MID function to search for a " " 275 characters out from the left. If a " " exists, then I want to write the contents of shortDesc up to that point, add ... and have the reader click on a link to view the entire contents of the event description. But, if MID(shortDesc, 275, 1) <> " ", then I want to search for the last instance of " " within the first 275 characters, add ... and have the reader click on a link to view the entire contents of the event description. Should I use the InStr to accomplish that?

This is what I am after:

Tuesday, October 25, 2007 10:00 AM to 12:00 PM read more...
This is only a test of the class calendar. This test is just full of blank, non-meaningful data that is entered to merely represent what a true calendar event for The Leadership Organization would look like. Hopefully, it represents well once in a productio...
at Westin Hotel, Somewhere



Any help would be most appreciated!

Thanks,
GC
 
Use InStrRev. The third parameter will be 275.
 
Thanks for the reply!

Perhaps I need some more assistance.

If I use this section of code:
Code:
shortDesc = objRSClassCalendar("shortDesc")
If MID(shortDesc, 275, 1) <> " " Then
     Response.Write "" & shortDesc & ""
EndIf
I get this
Code:
[b]
Tuesday, October 25, 2007        10:00 AM to 12:00 PM[/b]        read more... 
This is only a test of the class calendar. This test is just full of blank, non-meaningful data that is entered to merely represent what a true calendar event for Leadership Organization would look like. Hopefully, it represents well once in a productio... 
[b]at Westin Hotel, Somewhere[/b]

But, if I build out my conditional statements, nothing seems to print.

Code:
								shortDesc = objRSClassCalendar("shortDesc")
								If MID(shortDesc, 275, 1) = " " Then
										Response.Write "" & shortDesc & ""
								ElseIf MID(shortDesc, 275, 1) <> " " Then
									If InStrRev(shortDesc, " ", 275, 1) = " " Then
										Response.Write "" & shortDesc & ""
									End If
								End If

I get this:
Code:
Tuesday, October 25, 2007        10:00 AM to 12:00 PM        read more... 
... 
at Westin Hotel, Providence

I have been looking at this for awhile, so I apologize if I am missing something obviously blatant, but I need a second set of eyes with some experience at building this type of code (as this is the first time I have ever been asked to do this). I also have to use this coding elsewhere in the client's site, so any help would be greatly appreciated!

Thanks,
GC
 
As a compromise against real odd cases, you can do this.
[tt]
shortDesc = cstr(objRSClassCalendar("shortDesc"))
s=left(rtrim(shortDesc),275)
if len(s)<275 then
sdisplay=s
elseif instr(ltrim(s)," ")=0 then
sdisplay=s & "..."
else
n=instrrev(s," ")
sdisplay=rtrim(left(s,n-1)) & "..."
end if
response.write sdisplay
[/tt]
There may still be hole in it for odd cases, but those will be real odd.
 
use the split command to split the words in the field into separate words. then string them together into the line while subtracting length of each word + 1 (for space) from the length of the line.
 
Hello all!

Here is the function I used to get the site to do what I need:

Code:
Function GetFirstN(FieldIn, NumChars)
 Dim HoldLength
 ' Eliminate excessive spaces from end
 FieldIn = Trim(FieldIn)
 ' If Less than the maximum, just return the field
 If Len(FieldIn) <= NumChars then
  GetFirstN = FieldIn : Exit Function
 End If
 
 If mid(FieldIn, NumChars, 1) = " " then
  GetFirstN = Left(FieldIn, NumChars)
 Else 
	HoldLength = instr(NumChars + 1, FieldIn, " ")
  If HoldLength = 0 then HoldLength = instrrev(FieldIn, " ", NumChars) ' in case no spaces are after 
  If HoldLength = 0 then
	  ' no spaces found at all (doubtful but could happen), so take first numchars characters
   GetFirstN = Left(FieldIn, ",", NumChars)
  else
	  ' take length based on the appropriate space.
   GetFirstN = Left(FieldIn, HoldLength)
	End if
 End If
End Function

This is actually a modification of a function that I found. I have been trying to find the page or source so that I can give the proper credit to the person who designed it...but cannot right now. I will be sure to give the proper credit tonight when I get home.

Thanks to everyone who responded with their replies! Your help is much appreciated!

Thanks,
GC
 
Here's my version:
Code:
[blue]Public Function GetFirstN(FieldIn, NumChars)
    If InStr(FieldIn, " ") Then
        GetFirstN = Left(FieldIn, InStrRev(Left(FieldIn, NumChars), " ", NumChars))
    Else
        GetFirstN = Left(FieldIn, NumChars)
    End If
End Function[/blue]
 
with the split command, you would have this
let's say
your_field = "this is the memo field that you have".\
words = split(strMachines, " ")
words(s) will contain: "this", "is", "the", "memo", "field", "that",
"you", "have"
then you can use:
leng = 275
new_line = ""
for each_word in words
do while length < 275
new_line & each_word
if leng > 0 then
leng = leng - (len(eachword)+1)
end if
loop
next
 
This is what I'd use.

Code:
shortDesc = left(longDesc, 276)
While right(shortDesc, 1) <> " " and len(shortDesc) > 1
  shortDesc = left(shortDesc, len(shortDesc) - 1)
Wend
If len(shortDesc) > 0 then shortDesc = left(shortDesc, len(shortDesc) - 1)[code]
 
sorry, my mistake.
the code should look like this: (almost).
your_field = "this is the memo field that you have".\
words = split(your_fields, " ")
words(s) will contain: "this", "is", "the", "memo", "field", "that",
"you", "have"
then you can use:
leng = 275
new_line = ""
for each_word in words
do while length < 275
new_line = new_line & each_word
if leng > 0 then
leng = leng - (len(eachword)+1)
end if
loop
next
 
xwb-

I gave you a star, 'cause I learned a new command.

Certainly instrrev would be the way to solve this problem, since you can tell it to START at a particular column... so for example, if you wanted to find the first whitespace character AFTER x number of characters, you could do....
Code:
intCharPos = InStrRev(somereallylongstring," ",x,1)
... which would give you the first space after character "x".... then you would just....
Code:
response.write Left(somereallylongstring,intCharPos) & "..."
... Problem solved.

It's elegant, and works in two lines....

For that matter, it could work in ONE line....

Code:
response.write (Left(somereallylongstring,InStrRev(somereallylongstring," ",265))) & "..."

(I think that looks right... that would look for the first white space after character 265, return that as an integer, then chop off the left of the string, add ... afterwards.... I like it. :)



Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
that works fine (most of the time). but if you have a long word (10 chars or more), then you will exceed the 275 max.
 
Greg, good job making in a one liner, but yours will never return a 275 character string (even if your coding is changed to 275). If the last letter of a word is the 275th character, then it would return a string shorter than that.

"[StringOf270Characters] more text here" would return "[StringOf270Characters]..." when it should be "[StringOf270Characters] more" per the original request. I suppose I could be splitting hairs, but this would fit the requirments from the original post:
Code:
If InStrRev(strLong, " ", 276) then strShort = Left(strLong, InStrRev(strLong, " ", 276) - 1) Else strShort = ""
 
This has been one of the most impressive and intuitive posts that I have started on Tek-Tips. I certainly appreciate everyone's replies, tips and techniques on how to approach this and I have learned quite a bit about the MID, InStr and InStrRev functions in ASP, which is what I really find to be the most valuable part of posting here. (Not just how to copy and paste someone else's creation as a work around).

Thank you to everyone who replied to my post! I hope that we have all found something new and useful to take away from this!

GC
 
Well, chopping off at the 275'th character is EASY... my understanding was that we didn't want to chop in the middle of a word. ;) That's why I "backspaced" 10 characters, so that theoretically, it would find a whitespace before character 275, since the "average" word length in the english language is 5 characters.....



Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
One quick, minor alteration to my earlier post, to handle the scenario when you the requested break position is longer than the source text:
Code:
[blue]Public Function GetFirstN(FieldIn, NumChars)
    If InStr(FieldIn, " ") [b]And Len(FieldIn) >= NumChars[/b] Then
        GetFirstN = Left(FieldIn, InStrRev(Left(FieldIn, NumChars), " ", NumChars) - 1)
    Else
        GetFirstN = Left(FieldIn, NumChars)
    End If
End Function[/blue]
 
strongm,

I have never had to write a function of this magnitude, although it does work great. I have been studying your latest post, trying to break it down into smaller components so that I can understand exactly how it works, but am still uncertain as to how it does what it does.

I would sincerely appreciate it if you could break down how your function works in English so that I can really learn how it works and help pass on your knowledge to others as well as educate myself as to how it works.

Thanks,
GC
 
GC, The first variable is the string you want to crop and the second is where you want to crop it.
Code:
strLong = "123 567 90"
strShort = GetFirstN(strLong,5)
In this example, strLong becomes the variable FieldIn. 5 becomes the variable NumChars. strShort ends up the "123" which is the value returned by the function (the "GetFirstN =" parts).

By making it a function you can reuse it to crop any string to any length desired. So you don't have to do the following:
Code:
If InStr(strLong, " ") And Len(strLong) >= 50 Then
    strShort = Left(strLong, InStrRev(Left(strLong, 50), " ", 50) - 1)
Else
    strShort = Left(strLong, 50)
End If
If InStr(strLonger, " ") And Len(strLonger) >= 40 Then
    strShorter = Left(strLonger, InStrRev(Left(strLonger, 40), " ", 40) - 1)
Else
    strShorter = Left(strLonger, 40)
End If
If InStr(strLongest, " ") And Len(strLongest) >= 30 Then
    strShortest = Left(strLongest, InStrRev(Left(strLongest, 30), " ", 30) - 1)
Else
    strShortest = Left(strLongest, 30)
End If

Instead you can use:
Code:
strShort = GetFirstN(strLong,50)
strShorter = GetFirstN(strLonger,40)
strShortest = GetFirstN(strLongest,30)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top