在textFiedl(特定词)中搜索 [英] search in a textFiedl, a specific word

查看:102
本文介绍了在textFiedl(特定词)中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的AS3代码中,我有一个文本字段,可从数据库中加载句子(每个句子都是一个标题.

I've got, in my AS3 code, a textfield that load sentences from a database (each sentences is a title.

如何简单地为此TextField做一个搜索栏.当用户键入单词"computer"(例如)时,它将在每个句子中搜索单词"computer".

How can I simply do a search bar for this TextField. When the user type the word "computer" (for exemple), it'll search in every sentences the word "computer".

这是我当前的代码:

function createListItem(index:int, item:Object):void {
    var listItem:TextField = new TextField();
    var myFormat:TextFormat = new TextFormat();
    myFormat.size = 25
    myFormat.color = 0x000000;
    myFormat.font = "Mohave";
    listItem.defaultTextFormat = myFormat;
    listItem.text = item.title;
    listItem.x = 10;
    listItem.y = 140+ index * 40;
    listItem.width = 160;
    feedbackText.text = "createListItem" +item.title;
    listItem.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
        showDetails(item);
    });
    list.addChild(listItem);

}

并显示如下列表:

"I sell a computer"
"I sell a desk"
"computer and apple"

(并且知道我是否可以执行搜索栏以便仅显示具有特定单词的句子).

(and know if I can do a search bar in order to display only the sentences with a specific word).

感谢您的帮助

推荐答案

首先将您的列表转换成数组

First convert your list into an array

var str: String = "first sentence. second sentence. third sentence. fourth sentence";
var arr: Array  = str.split(".");/// to split the string into array items



function searching(val: String): String {
    var collectedString: String = "";
    for (var i: int = 0; i < arr.length; i++) {
        if (arr[i].search(val) > -1) {
            collectedString += arr[i].toString()+".";
        }
    }

    return collectedString;

}

trace(searching("second")); /// input the key word 
trace( searching("sentence")); /// another the key word 

这篇关于在textFiedl(特定词)中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆