增加方法:

1、添加新字符到指定位置

public static function addAt(char:String, value:String, position:int):String

2、替换指定位置字符

public static function replaceAt(char:String, value:String, beginIndex:int, endIndex:int):String

3、删除指定位置字符

public static function removeAt(char:String, beginIndex:int, endIndex:int):String

4、修复双换行符

public static function fixNewlines(char:String):String

  1. /**   
  2.  * @link http://www.klstudio.com   
  3.  * @author Kinglong 
  4.  * @playerversion flash player 9+  
  5.  * @asversion 3.0 
  6.  * @version 0.6 
  7.  * @builddate  20081023 
  8.  * @updatedate 20090106 
  9.  */    
  10.   
  11. package com.klstudio.util{  
  12.       
  13.     import flash.utils.ByteArray;  
  14.       
  15.     public class StringUtil{  
  16.                   
  17.         //忽略大小字母比较字符是否相等;  
  18.         public static function equalsIgnoreCase(char1:String,char2:String):Boolean{  
  19.             return char1.toLowerCase() == char2.toLowerCase();  
  20.         }  
  21.           
  22.         //比较字符是否相等;  
  23.         public static function equals(char1:String,char2:String):Boolean{  
  24.             return char1 == char2;  
  25.         }  
  26.           
  27.         //是否为Email地址;  
  28.         public static function isEmail(char:String):Boolean{  
  29.             if(char == null){  
  30.                 return false;  
  31.             }  
  32.             char = trim(char);  
  33.             var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;   
  34.             var result:Object = pattern.exec(char);  
  35.             if(result == null) {  
  36.                 return false;  
  37.             }  
  38.             return true;  
  39.         }  
  40.           
  41.         //是否是数值字符串;  
  42.         public static function isNumber(char:String):Boolean{  
  43.             if(char == null){  
  44.                 return false;  
  45.             }  
  46.             return !isNaN(Number(char));  
  47.         }  
  48.           
  49.         //是否为Double型数据;  
  50.         public static function isDouble(char:String):Boolean{  
  51.             char = trim(char);  
  52.             var pattern:RegExp = /^[-\+]?\d+(\.\d+)?$/;   
  53.             var result:Object = pattern.exec(char);  
  54.             if(result == null) {  
  55.                 return false;  
  56.             }  
  57.             return true;  
  58.         }  
  59.         //Integer;  
  60.         public static function isInteger(char:String):Boolean{  
  61.             if(char == null){  
  62.                 return false;  
  63.             }  
  64.             char = trim(char);  
  65.             var pattern:RegExp = /^[-\+]?\d+$/;   
  66.             var result:Object = pattern.exec(char);  
  67.             if(result == null) {  
  68.                 return false;  
  69.             }  
  70.             return true;  
  71.         }  
  72.         //English;  
  73.         public static function isEnglish(char:String):Boolean{  
  74.             if(char == null){  
  75.                 return false;  
  76.             }  
  77.             char = trim(char);  
  78.             var pattern:RegExp = /^[A-Za-z]+$/;   
  79.             var result:Object = pattern.exec(char);  
  80.             if(result == null) {  
  81.                 return false;  
  82.             }  
  83.             return true;  
  84.         }  
  85.         //中文;  
  86.         public static function isChinese(char:String):Boolean{  
  87.             if(char == null){  
  88.                 return false;  
  89.             }  
  90.             char = trim(char);  
  91.             var pattern:RegExp = /^[\u0391-\uFFE5]+$/;   
  92.             var result:Object = pattern.exec(char);  
  93.             if(result == null) {  
  94.                 return false;  
  95.             }  
  96.             return true;  
  97.         }  
  98.         //双字节  
  99.         public static function isDoubleChar(char:String):Boolean{  
  100.             if(char == null){  
  101.                 return false;  
  102.             }  
  103.             char = trim(char);  
  104.             var pattern:RegExp = /^[^\x00-\xff]+$/;   
  105.             var result:Object = pattern.exec(char);  
  106.             if(result == null) {  
  107.                 return false;  
  108.             }  
  109.             return true;  
  110.         }  
  111.           
  112.         //含有中文字符  
  113.         public static function hasChineseChar(char:String):Boolean{  
  114.             if(char == null){  
  115.                 return false;  
  116.             }  
  117.             char = trim(char);  
  118.             var pattern:RegExp = /[^\x00-\xff]/;   
  119.             var result:Object = pattern.exec(char);  
  120.             if(result == null) {  
  121.                 return false;  
  122.             }  
  123.             return true;  
  124.         }  
  125.         //注册字符;  
  126.         public static function hasAccountChar(char:String,len:uint=15):Boolean{  
  127.             if(char == null){  
  128.                 return false;  
  129.             }  
  130.             if(len < 10){  
  131.                 len = 15;  
  132.             }  
  133.             char = trim(char);  
  134.             var pattern:RegExp = new RegExp("^[a-zA-Z0-9][a-zA-Z0-9_-]{0,"+len+"}$""");   
  135.             var result:Object = pattern.exec(char);  
  136.             if(result == null) {  
  137.                 return false;  
  138.             }  
  139.             return true;  
  140.         }  
  141.         //URL地址;  
  142.         public static function isURL(char:String):Boolean{  
  143.             if(char == null){  
  144.                 return false;  
  145.             }  
  146.             char = trim(char).toLowerCase();  
  147.             var pattern:RegExp = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;   
  148.             var result:Object = pattern.exec(char);  
  149.             if(result == null) {  
  150.                 return false;  
  151.             }  
  152.             return true;  
  153.         }  
  154.           
  155.         // 是否为空白;         
  156.         public static function isWhitespace(char:String):Boolean{  
  157.             switch (char){  
  158.                 case " ":  
  159.                 case "\t":  
  160.                 case "\r":  
  161.                 case "\n":  
  162.                 case "\f":  
  163.                     return true;      
  164.                 default:  
  165.                     return false;  
  166.             }  
  167.         }  
  168.           
  169.         //去左右空格;  
  170.         public static function trim(char:String):String{  
  171.             if(char == null){  
  172.                 return null;  
  173.             }  
  174.             return rtrim(ltrim(char));  
  175.         }  
  176.           
  177.         //去左空格;   
  178.         public static function ltrim(char:String):String{  
  179.             if(char == null){  
  180.                 return null;  
  181.             }  
  182.             var pattern:RegExp = /^\s*/;   
  183.             return char.replace(pattern,"");  
  184.         }  
  185.           
  186.         //去右空格;  
  187.         public static function rtrim(char:String):String{  
  188.             if(char == null){  
  189.                 return null;  
  190.             }  
  191.             var pattern:RegExp = /\s*$/;   
  192.             return char.replace(pattern,"");  
  193.         }  
  194.           
  195.         //是否为前缀字符串;  
  196.         public static function beginsWith(char:String, prefix:String):Boolean{            
  197.             return (prefix == char.substring(0, prefix.length));  
  198.         }  
  199.           
  200.         //是否为后缀字符串;  
  201.         public static function endsWith(char:String, suffix:String):Boolean{  
  202.             return (suffix == char.substring(char.length - suffix.length));  
  203.         }  
  204.           
  205.         //去除指定字符串;  
  206.         public static function remove(char:String,remove:String):String{  
  207.             return replace(char,remove,"");  
  208.         }  
  209.           
  210.         //字符串替换;  
  211.         public static function replace(char:String, replace:String, replaceWith:String):String{           
  212.             return char.split(replace).join(replaceWith);  
  213.         }  
  214.           
  215.         //utf16转utf8编码;  
  216.         public static function utf16to8(char:String):String{  
  217.             var out:Array = new Array();  
  218.             var len:uint = char.length;  
  219.             for(var i:uint=0;i<len;i++){  
  220.                 var c:int = char.charCodeAt(i);  
  221.                 if(c >= 0x0001 && c <= 0x007F){  
  222.                     out[i] = char.charAt(i);  
  223.                 } else if (c > 0x07FF) {  
  224.                     out[i] = String.fromCharCode(0xE0 | ((c >> 12) & 0x0F),  
  225.                                                  0x80 | ((c >>  6) & 0x3F),  
  226.                                                  0x80 | ((c >>  0) & 0x3F));  
  227.                 } else {  
  228.                     out[i] = String.fromCharCode(0xC0 | ((c >>  6) & 0x1F),  
  229.                                                  0x80 | ((c >>  0) & 0x3F));  
  230.                 }  
  231.             }  
  232.             return out.join('');  
  233.         }  
  234.           
  235.         //utf8转utf16编码;  
  236.         public static function utf8to16(char:String):String{  
  237.             var out:Array = new Array();  
  238.             var len:uint = char.length;  
  239.             var i:uint = 0;  
  240.             var char2:int,char3:int;  
  241.             while(i<len){  
  242.                 var c:int = char.charCodeAt(i++);  
  243.                 switch(c >> 4){  
  244.                     case 0case 1case 2case 3case 4case 5case 6case 7:  
  245.                         // 0xxxxxxx  
  246.                         out[out.length] = char.charAt(i-1);  
  247.                         break;  
  248.                     case 12case 13:  
  249.                         // 110x xxxx   10xx xxxx  
  250.                         char2 = char.charCodeAt(i++);  
  251.                         out[out.length] = String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));  
  252.                         break;  
  253.                     case 14:  
  254.                         // 1110 xxxx  10xx xxxx  10xx xxxx  
  255.                         char2 = char.charCodeAt(i++);  
  256.                         char3 = char.charCodeAt(i++);  
  257.                         out[out.length] = String.fromCharCode(((c & 0x0F) << 12) |  
  258.                             ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));  
  259.                         break;  
  260.                 }  
  261.             }  
  262.             return out.join('');  
  263.         }  
  264.           
  265.         //转换字符编码;  
  266.         public static function encodeCharset(char:String,charset:String):String{  
  267.             var bytes:ByteArray = new ByteArray();  
  268.             bytes.writeUTFBytes(char);  
  269.             bytes.position = 0;  
  270.             return bytes.readMultiByte(bytes.length,charset);  
  271.         }  
  272.           
  273.         //添加新字符到指定位置;         
  274.         public static function addAt(char:String, value:String, position:int):String {  
  275.             if (position > char.length) {  
  276.                 position = char.length;  
  277.             }  
  278.             var firstPart:String = char.substring(0, position);  
  279.             var secondPart:String = char.substring(position, char.length);  
  280.             return (firstPart + value + secondPart);  
  281.         }  
  282.           
  283.         //替换指定位置字符;  
  284.         public static function replaceAt(char:String, value:String, beginIndex:int, endIndex:int):String {  
  285.             beginIndex = Math.max(beginIndex, 0);             
  286.             endIndex = Math.min(endIndex, char.length);  
  287.             var firstPart:String = char.substr(0, beginIndex);  
  288.             var secondPart:String = char.substr(endIndex, char.length);  
  289.             return (firstPart + value + secondPart);  
  290.         }  
  291.           
  292.         //删除指定位置字符;  
  293.         public static function removeAt(char:String, beginIndex:int, endIndex:int):String {  
  294.             return StringUtil.replaceAt(char"", beginIndex, endIndex);  
  295.         }  
  296.           
  297.         //修复双换行符;  
  298.         public static function fixNewlines(char:String):String {  
  299.             return char.replace(/\r\n/gm, "\n");  
  300.         }  
  301.     }  
  302. }