hillspro
hillspro

Reputation: 765

as3 - add space after comma in dynamic text field

I am pulling in city and state names like this - Dallas,TX. I need to add a space after the comma. Is there a way to find the comma and add in the space after?

Upvotes: 0

Views: 1024

Answers (4)

Syed
Syed

Reputation: 79

This is a two year old post but here is the answer for future visitors.

Use quotes with a space and use the plus sign between characters.

for example:

trace("Dallas," + " " + "TX");  

or

this.location_text_field.text = String("Dallas," + " TX");

In the first example there is a space between two quotes in the middle In the second example the space is after Dallas, and before TX

Upvotes: 0

Benny
Benny

Reputation: 2228

package  {
    public class Main extends Sprite {      
        private var _txt:TextField;
        public function Main() {
            // constructor code
            _txt = new TextField();
            _txt.type = TextFieldType.INPUT;
            _txt.border = true;
            addChild(_txt);               
            _txt.addEventListener(TextEvent.TEXT_INPUT,onTextChange);
        }
        //onTextChange function will fire in every key press except for the Delete or Backspace keys.
        private function onTextChange(e:TextEvent):void{
             if(_txt.text.charAt(_txt.text.length-1) == ","){
                       //Appends the string specified by the _txt parameter to the end of the text field.
                        _txt.appendText("  ");
                        //setSelection - I have used for move the text cursor to end of the textField.
                            _txt.setSelection(_txt.length,_txt.length);
                        }
                    }
                }


        }

Upvotes: 0

Simon McArdle
Simon McArdle

Reputation: 893

You can use String.replace to replace any occurrences of "," with ", ", for example:

var example:String = "Dallas,TX";
example.replace(",", ", ");
// example now reads "Dallas, TX"

You can look here for more information on String and its member functions: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html

Upvotes: 1

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

Try replace :

var cityName:String = "Dallas,TX";
cityName.replace(",",", ");
trace(cityName);

Upvotes: 1

Related Questions