1

我试图弄清楚如何创建一个函数来“突出显示”给定字符串的子字符串。理想情况下,该函数将采用 3 个参数:Haystack String、Search/Needle String 和 New Format。

这是我到目前为止所拥有的:

/* 
 * Replaces all occurances of one series of characters with another series of characters in correct TextFormat
 *
 * @param The haystack: TextField you are invoking the replace on.
 * @param The needle: Series of characters which you plan on replacing
 * @param The new format of the sub-string.
 * @return null
 * 
 * @usage <pre>StringUtility.highlight("Hello World", "World", "Michael");</pre>
 */ 
public static function highlight( block:TextField, search:String, appliedFormat:TextFormat ) {
    var original:String = block.text;
    var positions:Object = StringUtility.getPostitions( original, search );
    block.setTextFormat( value.appliedFormat, positions.posStart, positions.posEnd );
}

/* 
 * Replaces all occurances of one series of characters with another series of characters
 *
 * @param The haystack: string you are invoking the getPostitions on.
 * @param The needle: series of characters which you plan on measuring
 * @return The object with position values
 * 
 * @usage <pre>StringUtility.getPostitions("Hello World", "World", "Michael");</pre>
 */
public static function getPostitions(original:String, search:String) : String {
    var return_string  : String = "";
    var start_position : Number = 0;
    var end_position   : Number = 0;

    while (true) {
        start_position = original.indexOf( search, end_position );

        if ( -1 == start_position ) break;

        return_string += original.substring( end_position, start_position );
        return_string += search;

        end_position = start_position + search.length;
    }

    return var positions:Object = {posStart:start_position, posEnd:end_position};
}
4

1 回答 1

3

有趣的是,我正要深入研究这个,因为我需要它来做一个即将到来的项目。感谢分享。我对它进行了一些修改,所以它可以拾取所有出现的情况。

StringUtils.as

package  {

    import flash.text.*;

    public class StringUtils {

        public function StringUtils() {}

        public static function highlight(block:TextField, search:String, appliedFormat:TextFormat) {

            var positions:Array = getPositions(block.text, search);
            var len:uint = positions.length;

            for(var i:int = 0; i<len; i++){
                block.setTextFormat(appliedFormat, positions[i].posStart, positions[i].posEnd);
            }
        }

        public static function getPositions(original:String, search:String):Array {

            var positions:Array = [];
            var startPosition:Number;
            var endPosition:Number;

            while (startPosition != -1) {
                startPosition = original.indexOf(search, endPosition);
                endPosition = startPosition + search.length;
                if(startPosition > -1) positions.push({posStart:startPosition, posEnd:endPosition});
            }

            return positions;
        }
    }
}

StringUtils.fla 有一个名为 'txt' 的文本字段和文本 'Yo check out dude,checkcheck!!' 在它的舞台上

import StringUtils;
import flash.text.TextFormat;

var tf:TextFormat = new TextFormat();
tf.color = 0xFF0000;

StringUtils.highlight(txt, "check", tf);
于 2012-08-23T21:32:17.550 回答