0

我正在输入这样的城市和州名 - 德克萨斯州达拉斯。我需要在逗号后添加一个空格。有没有办法找到逗号并在后面添加空格?

4

4 回答 4

1

尝试替换:

var cityName:String = "Dallas,TX";
cityName.replace(",",", ");
trace(cityName);
于 2012-10-04T15:48:29.390 回答
1

您可以使用String.replace“,”替换任何出现的“,”,例如:

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

您可以在此处查看有关 String 及其成员函数的更多信息:http: //help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html

于 2012-10-04T15:50:45.390 回答
0
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);
                        }
                    }
                }


        }
于 2012-10-04T17:56:32.130 回答
0

这是一个两年前的帖子,但这是未来访客的答案。

使用带空格的引号并在字符之间使用加号。

例如:

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

或者

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

在第一个示例中,中间的两个引号之间有一个空格 在第二个示例中,空格位于 Dallas 之后和 TX 之前

于 2014-08-20T00:31:58.820 回答