Re: [問題] 統計字數時,如何過濾掉空白字元

看板Flash作者 (不行喔五樓)時間17年前 (2008/11/21 01:36), 編輯推噓2(205)
留言7則, 2人參與, 最新討論串2/3 (看更多)
做了兩種方法,原理差不多 第一個: //版面設置免看--txt_main是輸入區塊 var txt_main:TextField=new TextField(); this.addChild(txt_main); txt_main.type=TextFieldType.INPUT; txt_main.width=200;txt_main.height=20; txt_main.x=100; txt_main.y=100; txt_main.border=true; var txt_true:TextField=new TextField(); this.addChild(txt_true); txt_true.width=200;txt_true.height=20; txt_true.x=txt_main.x; txt_true.y=txt_main.y-txt_main.height-10; txt_true.border=true; var txt_total:TextField=new TextField(); this.addChild(txt_total); txt_total.width=200;txt_total.height=20; txt_total.x=txt_true.x; txt_total.y=txt_true.y-txt_true.height-10; txt_total.border=true; //從這看就好 this.addEventListener(Event.ENTER_FRAME,checkType); function checkType(event:Event):void { var str:String=txt_main.text; var arr:Array=new Array(); arr=str.split(""); var true_total:int=arr.length; var match:String=" "; for(var i=0;i<arr.length;i++) { if(match==arr[i]) { true_total--; } } txt_total.text="目前長度:"+arr.length; txt_true.text="實際長度:"+true_total; } 第二個: //版面設置免看--txt_main是輸入區塊 var txt_main:TextField=new TextField(); this.addChild(txt_main); txt_main.type=TextFieldType.INPUT; txt_main.width=200;txt_main.height=20; txt_main.x=100; txt_main.y=100; txt_main.border=true; var txt_true:TextField=new TextField(); this.addChild(txt_true); txt_true.width=200;txt_true.height=20; txt_true.x=txt_main.x; txt_true.y=txt_main.y-txt_main.height-10; txt_true.border=true; var txt_total:TextField=new TextField(); this.addChild(txt_total); txt_total.width=200;txt_total.height=20; txt_total.x=txt_true.x; txt_total.y=txt_true.y-txt_true.height-10; txt_total.border=true; //從這看就好~ txt_main.addEventListener(KeyboardEvent.KEY_DOWN,checkType); function checkType(event:KeyboardEvent):void { var timer:Timer=new Timer(500,1); timer.addEventListener(TimerEvent.TIMER,checkit); timer.start(); } function checkit(event:TimerEvent):void { var str:String=txt_main.text; var j:Number=0; var count:int=0; while(str.indexOf(" ",j)!=-1) { count++; var i:int=str.indexOf(" ",j); j=i+1; } txt_total.text="目前字數:"+txt_main.length; txt_true.text="實際字數:"+(txt_main.length-count); event.target.removeEventListener(TimerEvent.TIMER,checkit); } 有問題再問.. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.121.185.58

11/21 12:20, , 1F
感謝klit1987大提供...我會試看看的..Orz
11/21 12:20, 1F

11/22 16:27, , 2F
我想問一下..因為我是使用flex裡頭的textArea組件讓人輸
11/22 16:27, 2F

11/22 16:28, , 3F
入文字..我套用的上面的碼解決了按空白鍵不計算字數問題
11/22 16:28, 3F

11/22 16:29, , 4F
但似乎按ENTER鍵產生的空白.仍然會計算1個字數.
11/22 16:29, 4F

11/22 16:30, , 5F
我覺得很納悶..這兩者產生的空白不是一樣..但為何ENTER會
11/22 16:30, 5F

11/22 16:31, , 6F
被計算..而空白鍵產生的空白可以過濾掉
11/22 16:31, 6F

11/23 10:13, , 7F
enter字元跟空白字元的keyCode不一樣啊
11/23 10:13, 7F
文章代碼(AID): #199Q0TUO (Flash)
文章代碼(AID): #199Q0TUO (Flash)