JS字符串函数扩展代码
2023-06-13 09:14:30 时间
/****************************************************
*CreateBy:joezhou
*CreateDate:2011-9-4
*Description:字符串辅助函数
****************************************************/
//String.prototype={
//caption:function(){
//},
//leftPad:function(padChar,width){
//if(this.length>=width){
//returnthis;
//}
//}
//};
String.prototype.padLeft=function(padChar,width){
varret=this;
while(ret.length<width){
if(ret.length+padChar.length<width){
ret=padChar+ret;
}
else{
ret=padChar.substring(0,width-ret.length)+ret;
}
}
returnret;
};
String.prototype.padRight=function(padChar,width){
varret=this;
while(ret.length<width){
if(ret.length+padChar.length<width){
ret+=padChar;
}
else{
ret+=padChar.substring(0,width-ret.length);
}
}
returnret;
};
String.prototype.trim=function(){
returnthis.replace(/^\s+/,"").replace(/\s+$/,"");
};
String.prototype.trimLeft=function(){
returnthis.replace(/^\s+/,"");
};
String.prototype.trimRight=function(){
returnthis.replace(/\s+$/,"");
};
String.prototype.caption=function(){
if(this){
returnthis.charAt(0).toUpperCase()+this.substr(1);
}
returnthis;
};
String.prototype.reverse=function(){
varret="";
for(vari=this.length-1;i>=0;i--){
ret+=this.charAt(i);
}
returnret;
};
String.prototype.startWith=function(compareValue,ignoreCase){
if(ignoreCase){
returnthis.toLowerCase().indexOf(compareValue.toLowerCase())==0;
}
returnthis.indexOf(compareValue)==0
};
String.prototype.endWith=function(compareValue,ignoreCase){
if(ignoreCase){
returnthis.toLowerCase().lastIndexOf(compareValue.toLowerCase())==this.length-compareValue.length;
}
returnthis.lastIndexOf(compareValue)==this.length-compareValue.length;
};
相关文章
- Fabric.js 使用纯色遮挡画布(前景色)
- html如何只刷新页面指定,js控制页面刷新 JS刷新当前页面的几种方法总结
- php 中js跳转页面跳转页面,js跳转代码_PHP页面跳转 Js页面跳转代码[通俗易懂]
- JavaScript基本知识点——带你逐步解开JS的神秘面纱
- js中四舍五入的方法_JS取整
- js字符串转date类型_java字符串转double
- 个人笔记(js+css篇一)
- 【JS 逆向百例】猿人学系列 web 比赛第二题:js 混淆 - 动态 cookie,详细剖析
- js 快速将日期格式转换为时间戳
- 浅谈android和js的交互问题
- Ember.Js v2.14.0-beta.1 发布,一个JavaScript框架详解编程语言
- 通过 JavaScript 实现MySQL数据库连接(js连接mysql)
- 快速掌握JS操作MySQL数据库技巧(js操作mysql数据库)
- 数据JS技术实现实时获取Oracle数据(js实时获取oracle)
- 从前端JS里请求Redis资源,搭建高性能应用(前端js请求redis)
- js目录列举函数
- CSS和JS标签style属性对照表(方便js开发的朋友)
- 封装了一个js图片轮换效果的函数
- JS定时关闭窗口的实例
- js判断游览器类型及版本号的代码
- js中自定义方法实现停留几秒sleep
- 原生js实现复制对象、扩展对象类似jquery中的extend()方法