Skip to main content
October 11, 2011
Answered

How to convert input text field data into a string i allready have.

  • October 11, 2011
  • 1 reply
  • 930 views

I have a string that searches the Twitter API, for keywords, for example :

var url:String = "http://search.twitter.com/search.json?q=Robin%20&lang=en&rpp=100";

I also have a input textfield dynamically created:

var inputField:TextField = new TextField();

addChild(inputField);

inputField.border = true;

inputField.width = 200;

inputField.height = 150;

inputField.x = 75;

inputField.y = 50;

inputField.type = "input";

inputField.multiline = true;

inputField.restrict = "A-Za-z0-9";

stage.focus = inputField;

But how do i take the data from the input text field and add it to the string, keep in mind the input data has to go in the keyword part for the string. In this example the keyword is "Robin"

This topic has been closed for replies.
Correct answer Ned Murphy

Thanks ned. this has helped alot. But how can i get it so the new string data is what is processed by the input text.


It would be put into place as shown below...

url = url.replace(regexp, "?q="+yourTextField.text+"&");

There are other ways to do this involving String methods, where you can first split the string on the "?q=", then split the second element of that result on the "&", then replace the first element of that second split with your textfield text and then sew the pieces back together as one string. 

1 reply

Ned Murphy
Legend
October 11, 2011

Are you saying you want to substitute whatever is between ?q and the first & with whatever is in the input textfield?

Do you realize that the %20 (a space character, I believe) is also part of the q value?

October 11, 2011

yeah i think so. Yeah i knew the %20 meant space bar, there was two words there before.

Yeah i just want the data from the text field to be var url:String = "http://search.twitter.com/search.json?q=____________&lang=en&rpp=100";

So yes what was after the ?q=.

Ned Murphy
Legend
October 11, 2011

You can probably use regular expressions to do this, though I am not too keen on the exact pattern you will need to isolate that portion of the string.  I tried to get something working for it, but it is getting caught up with not being able to stop at the first &...

var url:String = "http://search.twitter.com/search.json?q=Robin%20&lang=en&rpp=100";

var regexp:RegExp = new RegExp();

regexp = /\?q=.*&/;

trace(regexp.exec(url)); // this shows what it found

url = url.replace(regexp, "?q="+"YOUR NEW STRING"+"&");

trace(url); // this shows the result

Maybe someone else knows the key to getting it to stop at the first &, or a different pattern altogether that can work.