Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
Director 8 and Lingo: Inside Macromedia Series
Risk
replace escape characters
Get Font List without using xtras!
XML TextRender v1.3
HTML Xtra
Drag and Droop
SetMouseXtra
Toggle DirectToStage Property of a Flash Member
Sprite Stick To Mouse Cursor
 

 

 

Behavior Find all occurances of a string in a Text member

Added on 4/28/2000

 

Compatibilities:
D7 D8 Mac PC Script Shockwave

Rating:

Author: MarkBoyd (website)

This movie script will search & optionally highlight all occurances of a string in a text member. It will return a property list of positions with a hit count ie:[#hitPositions:[58,151],#hits:2] hitList = findAllChars("string","textMember",{rgb(0,0,255)}) The color parameter is optional.

-- Written by Mark A. Boyd
-- No copyright since I"ve posted variants of this script all over the place.
-- Please send questions/comments/improvements to mblists@san.rr.com
-- A working example is posted at
http://www.mboyd.com/director/findallchars.html

-- 27-Apr-2000 v1.1
-- Modified first repeat loop for indefinite iterations.

on findAllChars whichString, whichMember, whichColor

   stringCount = whichString.length - 1
   theText = member(WhichMember).text
   textCount = theText.length
   outList = [#hitPositions:[],#hits:0]
   charColorList = []
   currentOffset = 0

   -- Begin Search
   flag = 0
   repeat while not flag
     -- offset() returns only the first position
     firstPos = offset(whichString, theText)
     if firstPos then
       -- get the position of the last char in the search string.
       lastPos = firstPos + stringCount

       -- remember position offsets from original, unmodified text.
       newFirstPos = firstPos + currentOffset
       newLastPos = newFirstPos + stringCount

       -- add positions to temporary list for coloring.
       -- also used in counting the number of hits.
       charColorList.add([newFirstPos,newLastPos])

       -- append positions to the output list.
       outList.hitPositions.append(firstPos+currentOffset)

       -- delete up to the last position so that offset()
       -- can find the next position.
       theText = theText.char[lastPos + 1..textCount]
       currentOffset = currentOffset + lastPos
       textCount = theText.length
     else
       -- no more hits from offset()
       flag = 1
     end if
   end repeat

   -- Begin highlighting (if color parameter provided)
   if not whichColor.voidP then
     listCount = charColorList.count
     if listCount then
       repeat with i = 1 to listCount
         theChars = charColorList[i]
         member(whichMember).char[theChars[1]..theChars[2]].color = whichColor
       end repeat
     end if
   end if

   outList.hits = charColorList.count
   return outList
end

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail