- public function LTrim(s : String):String
- {
- var i : Number = 0;
- while(s.charCodeAt(i) == 32 || s.charCodeAt(i) == 13 || s.charCodeAt(i) == 10 || s.charCodeAt(i) == 9)
- {
- i++;
- }
- return s.substring(i,s.length);
- }
-
- public function RTrim(s : String):String
- {
- var i : Number = s.length - 1;
- while(s.charCodeAt(i) == 32 || s.charCodeAt(i) == 13 || s.charCodeAt(i) == 10 ||s.charCodeAt(i) == 9)
- {
- i--;
- }
- return s.substring(0,i+1);
- }
-
- public function Trim(s : String):String
- {
- return LTrim(RTrim(s));
- }
- 直接使用正则表达式
-
- public function trim(s : String):String {
- return s.replace(/([ ]{0,})/g,"");
- }
- 让格式更严格一点
-
- public function trim(s : String):String {
- return s.replace(/([ ]{1})/g,"");
- }
- 简化版--
-
- public function LTrim(s : String):String
- {
- return ((s.charCodeAt(0) == 32 || s.charCodeAt(0) == 13 || s.charCodeAt(0) == 10 || s.charCodeAt(0) == 9)?LTrim(s.substring(1,s.length)):s);
- }
-
- public function RTrim(s : String):String
- {
- return ((s.charCodeAt(s.length-1) == 32 || s.charCodeAt(s.length-1) == 13 || s.charCodeAt(s.length-1) == 10 || s.charCodeAt(s.length-1) == 9)?RTrim(s.substring(0,s.length-1)):s);
- }
-
- public function Trim(s : String):String
- {
- return LTrim(RTrim(s));
- }
- 好吧,简单而又强大的方法,我马上去试试,先收藏在这里了。
posted @
2012-01-09 00:13
rob_2010
阅读(
223)
评论()
编辑
收藏
举报