- String.prototype.trim = function() {
- return this.replace(/^\s+|\s+$/g,"");
- }
- String.prototype.ltrim = function() {
- return this.replace(/^\s+/g,"");
- }
- String.prototype.rtrim = function() {
- return this.replace(/\s+$/g,"");
- }
- String.prototype.splitAndTrim = function($delimiter, $limit)
- {
- var $ss = this.split($delimiter, $limit);
- for(var $i=0; $i<$ss.length; $i++)
- $ss[$i] = $ss[$i].trim();
- return $ss;
- }
- String.prototype.htmlEntities = function () {
- return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
- }
- String.prototype.stripTags = function () {
- return this.replace(/<([^>]+)>/g,'');
- }
- String.prototype.toArray = function() {
- return this.split('');
- }
- String.prototype.toIntArray = function() {
- var returnArray = [];
- for (var i=0; i<this.length; i++) {
- returnArray.push(this.charCodeAt(i));
- }
- return returnArray;
- }
- String.prototype.replaceAll = function($old, $snew){
- return this.replace(new RegExp($old,"gm"),$snew);
- }
- //变量替换var a = "I Love {0}, and You Love {1},Where are {0}!";a.format("You","Me");
- String.prototype.format = function(){
- var args = arguments;
- return this.replace(/\{(\d+)\}/g,function(m,i,o,n){
- return args[i];
- });
- }
- //在字符串末尾追加字符串
- String.prototype.append = function($str){
- return this.concat($str);
- }
- //删除指定索引位置的字符,索引无效将不删除任何字符
- String.prototype.deleteCharAt = function($sIndex){
- if($sIndex<0 || $sIndex>=this.length){
- return this.valueOf();
- }else if($sIndex==0){
- return this.substring(1,this.length);
- }else if($sIndex==this.length-1){
- return this.substring(0,this.length-1);
- }else{
- return this.substring(0,$sIndex)+this.substring($sIndex+1);
- }
- }
- //删除指定索引间的字符串.$sIndex和$eIndex所在的字符不被删除!依赖deleteCharAt
- String.prototype.deleteString = function($sIndex, $eIndex){
- if($sIndex==$eIndex){
- return this.deleteCharAt($sIndex);
- }else{
- if($sIndex>$eIndex){
- var tIndex=$eIndex;
- $eIndex=$sIndex;
- $sIndex=tIndex;
- }
- if($sIndex<0)$sIndex=0;
- if($eIndex>this.length-1)$eIndex=this.length-1;
- return this.substring(0,$sIndex+1)+this.substring($eIndex,this.length);
- }
- }
- //检查字符串是否以某个字符串(str)结尾
- String.prototype.endsWith = function($str){
- return this.substr(this.length - $str.length) == $str;
- }
- //检查该字符串是否以某个字符串开始
- String.prototype.startsWith = function(str){
- return this.substr(0, str.length) == str;
- }
- //比较两个字符串是否相等,不区分大小写!
- String.prototype.equalsIgnoreCase = function($str){
- if(this.length!=$str.length){
- return false;
- }else{
- var tmp1=this.toLowerCase();
- var tmp2=$str.toLowerCase();
- return tmp1==tmp2;
- }
- }
- //将指定的字符串插入到指定的位置后面!索引无效将直接追加到字符串的末尾
- String.prototype.insert = function($ofset, $str){
- if($ofset<0 || $ofset>=this.length-1){
- return this.concat($str);
- }
- return this.substring(0,$ofset)+$str+this.substring($ofset+1);
- }
- //将指定的位置的字符设置为另外指定的字符或字符串.索引无效将直接返回不做任何处理!
- String.prototype.setCharAt = function($ofset, $str){
- if($ofset<0 || $ofset>=this.length-1){
- return this.valueOf();
- }
- return this.substring(0,$ofset)+$str+this.substring($ofset+1);
- }
- String.prototype.replaceLen = function(start, len, replaced) {
- if(!len)
- return this;
- if(start >= this.length)
- return this;
- var returnSeg = '';
- var returnSeg2 = '';
- var i = 0;
- for (; i < this.length; i++){
- var c = this.charAt(i);
- if(i < start)
- returnSeg += c;
- if(i >= start + len)
- returnSeg2 += c;
- }
- return returnSeg + replaced + returnSeg2;
- }
- /**
- * 扩展基础类
- * 替换字符,这个在替换填入比较有用,比如***天***小时 替换为 <input />天<input />小时
- **/
- String.prototype.replaceChar = function(target, replaced, start) {
- if(!target)
- return this;
- if(!start)
- start = 0;
- var returnVal = this.substring(0, start);
- var index = 0;
- for (var i = start; i < this.length; i++) {
- var c = this.charAt(i);
- target = typeof target == 'function' ? target.call(this, index) : target;
- if (c == target) {
- returnVal += typeof replaced == 'function' ? replaced.call(this, index) : replaced;
- while (i < this.length - 1 && this.charAt(i + 1) == c) {
- i++;
- }
- index++;
- }else{
- returnVal += c;
- }
- }
- return returnVal;
- }
- //将该字符串反序排列
- String.prototype.reverse = function(){
- var str="";
- for(var i=this.length-1;i>=0;i--){
- str=str.concat(this.charAt(i));
- }
- return str;
- }
- //计算长度,每个汉字占两个长度,英文字符每个占一个长度
- String.prototype.ucLength = function(){
- var len = 0;
- for(var i=0;i<this.length;i++){
- if(this.charCodeAt(i)>255)len+=2;
- else len++;
- }
- return len;
- }
- //在字符串的左边填充一些特定的字符
- String.prototype.lpad = function(len, s) {
- var a = new Array(this);
- var n = (len - this.length);
- for ( var i = 0; i < n; i++) {
- a.unshift(s);
- }
- return a.join("");
- }
- //在字符串的右边填充一些特定的字符
- String.prototype.rpad = function(len, s) {
- var a = new Array(this);
- var n = (len - this.length);
- for ( var i = 0; i < n; i++) {
- a.push(s);
- }
- return a.join("");
- }
- //把字符串的首字母转化为大写
- String.prototype.ucwords = function() {
- return this.substring(0,1).toUpperCase().concat(this.substring(1));
- }
- String.prototype.contains = function($str) {
- return this.indexOf($str) > -1 ? true : false;
- }
- //将格式为2008-04-02 10:08:44的字符串转成日期(String对象的值必须为: 2008-04-02 10:08:44)
- String.prototype.toDate = function(){
- var str = this.replace(/-/g,"/");
- return (new Date(str));
- }
- //将原来用字符串表示的十进数转成十进制浮点数: precision为精度
- String.prototype.toFloat = function(precision){
- precision = precision || 2;
- return parseFloat(this,10).toFixed(precision);
- }
- //将原来用字符串表示的十进数转成十进制整数
- String.prototype.toInt = function(){
- return parseInt(this,10).toString();
- }
- //将两个原来用字符串表示的十进数相加后当作字串返回 : addend为加数
- String.prototype.add = function(addend){
- var sum = parseFloat(this,10) + parseFloat(addend,10);
- return sum+"";
- }
- //十进制转其他进制代码如下nextScale为进制 如2,8,16
- String.prototype.shiftScale = function(nextScale){
- return parseFloat(this).toString(nextScale);
- }
- /**
- * 各进制互相转换
- * this对象必须是整数
- * @param preScale 原是是几进制数
- * @param nextScale 要转换成几进制数
- */
- String.prototype.scaleShift = function(preScale,nextScale){
- return parseInt(this,preScale).toString(nextScale);
- }
- //全角2半角 document.write("ABC 123,我们都是好朋友");
- String.prototype.dbc2sbc = function (){
- return this.replace(/[\uff01-\uff5e]/g,function(a){return String.fromCharCode(a.charCodeAt(0)-65248);}).replace(/\u3000/g," ");
- }
js String对象的扩展函数
最新推荐文章于 2022-06-21 01:49:54 发布