PDA

View Full Version : how to find a world in a textfield


issamneo
02-09-2005, 02:28 PM
hi everybody,
i have a text field i want to search a word and change it's color,
to change it's color i use TextField.setTextFormat
but how will i find the start index and the end index of the word that i want to change it?

rickslate
02-09-2005, 02:36 PM
I think this might work. It uses a button called "find_btn"

find_btn.onRelease=function(){
var result:Number = inputField_txt.text.indexOf(findField_txt.text);
if (findField_txt.text !=""&& result>=0) {
Selection.setFocus("_root.inputField_txt");
Selection.setSelection(result,result+findField_txt .text.length);
//put your text change command here
};

issamneo
02-09-2005, 02:37 PM
hi everybody,
i have a text field i want to search a word and change it's color,
to change it's color i use TextField.setTextFormat
but how will i find the start index and the end index of the word that i want to change it?

Curly Brace
02-09-2005, 02:51 PM
Use this:

start_index = myString.indexOf(myWord);
end_index = start_index+myWord.length;

If that won't work try adding +1 or -1 to the end_index


EDIT: Looks like you've posted twice the same question... and another copy of you questioin had already been answered :) Please, don't duplicate your posts in feature.

dej4n
02-09-2005, 03:08 PM
i.e.
function getIndexes(str:String, key:String):Array{
var indexes = [];
var tmp = str.split(key);

var before = 0;
for (var i=0; i<tmp.length-1; i++){
indexes.push( tmp[i].length + before);
before += key.length+tmp[i].length;
}
return indexes;
}



key = "WWW";
str = "jkhfkdjWWWhfklasjdhflkWWWhjaslkfjhasWWWldhflhsadlk gWWWh";
// 01234567890123456789012345678901234567890123456789 0123456789
// 1 2 3 4 5

trace(getIndexis(str, key))

issamneo
02-09-2005, 03:32 PM
thanks for all
special thanks to dej4n
by the way it is trace(getIndexes(str, key)) not trace(getIndexis(str,key))
:D

sureshdavid
09-04-2008, 08:08 AM
Wow its exactly satisfy my requirement,
ok thanks..

But I want trace the all words with color change in the searched word...

Ist possible to achieve?.....




the req output is.....

jkhfkdjWWWhfklasjdhflkWWWhjaslkfjhasWWWldhflhsadlk gWWWh




My search is stop at this thread with waiting to get the solution.... Please help me..

Suresh J David

Canazza
09-05-2008, 11:20 AM
Find and Highlight Text

var HighlightFormat:TextFormat = new TextFormat()
HighlightFormat.color = 0xFF0000
HighlightFormat.underline = true

function highlightText(search_str) {
var HighlightStart = myTextField.text.toLowerCase().indexOf(search_str. toLowerCase(),0)
if(HighlightStart > -1)
myTextField.setTextFormat(HighlightStart,Highlight Start+search_str.length,HighlightFormat)



}

highlightText("WWW")

sureshdavid
09-05-2008, 11:39 AM
Thanks cannaza,

But Its diaplay like

jkhfkdjWWWhfklasjdhflkWWWhjaslkfjhasWWWldhflhsadlk gWWWh only,



But I need it view like this

jkhfkdjWWWhfklasjdhflkWWWhjaslkfjhasWWWldhflhsadlk gWWWh

I'm happy to we near to ahieve,
Waiting..... for your reply